Fog Creek Software
Discussion Board




Perl CGI

Is there a lot of Perl CGI scripting going on nowadays?  It seems very cumbersome to me, but I've only written one application so far.

anon
Thursday, July 15, 2004

Funny, I find Perl CGI to be the most agile of many web scripting options.  I usually fall back on it when it's taking to long to solve a problem in another language.

muppet
Thursday, July 15, 2004

Cumbersome how? Compared to what? I find it incredibly easy to build productive Web functionality quickly using Perl CGI, but that might just be a function of experience and familiarity.

John C.
Thursday, July 15, 2004

********* WARNING *********
These are my humble, humble
opinions, and are therefore
not flame bait!
***************************

>> "Cumbersome how? Compared to what?"

I'd rather use PHP or JSP or ASP or something like that.  It seems really messy to me that my code has to generate the HTML.  I'd rather just make an HTML file and fill in the blanks with the scripting.

But I'm new to web development!

Also, what's the big whoop with scripting languages?  Does it really save people so much time not having to declare variables & types?  I like strong typing.  In the end it saves much more of my time. 

anon
Thursday, July 15, 2004

CGI (in the traditional sense) just isn't a good platform to write whole web apps on, as the webserver has to launch a separate process for each CGI request.

So in that sense not many people are writing CGI apps, let alone Perl CGI ones. Rather using modules built into the webserver like mod_perl, mod_php4, mod_python etc. Or FastCGI which is subtly different to vanilla CGI. The only time I really see Perl CGI being used is things like formmail.pl and that really ancient message board script whose name I forget. Essentially slightly dated bolt-on interactive bits for static sites.

Perl CGI was the first language/platform a lot of us learned for web development back in the day - I really wouldn't choose to use it now though, and I don't see many others writing Perl for the web these days. Despite PHP's numerous shortcomings 99% of the things a simple web app needs to do are much easier to write and maintain in PHP, than Perl.

If you're looking for real elegance try one of the many Python-based web platforms. Or just use Python for CGI scripting if you're okay with the performance hit. Python is a lovely language to work with, much prettier more elegant and consistent than Perl or PHP.

Matt
Thursday, July 15, 2004

Matt-

PHP's many shortcomings....

-like?


I actually enjoy coding in PHP a great deal, and I've yet to encounter something I flat out cannot do with a combination of PHP and Javascript.


As for "having to generate my html", I actually enjoy the control of outputting HTML how and when I want to, and the ability to parse and reparse it before sending it off to the browser.  I think Perl is very suited to this through regexps.  You can use perl regexps in PHP, but the syntax is clumsy.

When I write PHP I still don't inline with HTML most of the time (I've only recently started experimenting with it at all), but rather build my output string progressively in code and then echo it when I'm good and ready.

muppet
Thursday, July 15, 2004

PHP's shortcomings... hm.

(NB: I recognise some of these are fixed / improved in PHP5, or but barely anybody offers PHP5 hosting so I've not had the chance to work with it yet)

-Very inconsistently named builtin functions
-Separate sets of builtin functions for each database type
-Object oriented features are pathetic compared with any proper OO language. No multiple inheritance, interfaces, static methods, etc etc etc
-Essentially only one built-in data structure, which works as both a list and an associative array - some may see this as an advantage, I don't
-Lack of namespaces or modules
-Default behaviour is to copy objects rather than pass references, which can be hard/clumsy to get around, especially for the elements of an array when doing a foreach loop
-Generally gives the impression of being haphazardly designed if at all

Despite the above, though, it does make small-to-medium scale web development very easy, and I do tend to end up using it quite a bit.

Matt
Thursday, July 15, 2004

Personally I find the lack of multiple inheritance  a godsend.  What a muddled, useless mess.

I like the data structures just fine, too.

I'll give you the badly named internal functions, and the lack of namespaces.

muppet
Thursday, July 15, 2004

I know multiple inheritance can lead to a mess, but I feel I know how to use it responsibly and therefore am annoyed by languages which try to deny me the opportunity to use it.

Say if you have a class Foo, and you've written two orthogonal extensions to that class, Foo_with_X and Foo_with_Y. You then realise you also need a Foo_with_X_and_Y. With multiple inheritance in Python this sort of thing is a breeze using MixIn classes, but in PHP you either end up doing a lot of messy refactoring, or just copying and pasting the two pieces of code into a third class extending Foo (which is obviously a Bad Thing).

Matt
Thursday, July 15, 2004

Actually what you guys are talking about just seems like a big mess.

The OP asked about Perl.  As one other astute poster mentioned, it is agile. You can put something simple togehter quite quickly and something quite complicated is also possible. CPAN.

I've heard it said that Perl is easy and it is hard. I reckon if you came up from C it should be pretty simple. If you know PHP it should be simple.  If you are an IDE programmer it might be hell on wheels. We got a java consultant who ... honest to god, is writing code in notepad. The other full-time java people say he doesn't know their IDE or can't get it setup or something and can't use his so he's just using notepad. Oh my god. How in the world could a programmer ever consider to use notepad.  You can get both vi or emacs (my preference) for win32.

And really, what's better than this:

print qq|

"FirstName" <b>$firstname</b>
<h2>$middle_names_exist</h2>

|;

as opposed to

echo "\"FirstName\"<b><?=$firstname?></b>
<h2><?=$middle_names_exist?></h2>

";

Ah ... give me Perl ANY day. of course you could do this in PHP

print <<<HTML

"FirstName" <b>$firstname</b>
<h2>$middle_names_exist</h2>

HTML;

But YOU NEVER see anyone use the  heredoc .. nope, 99% of PHP programmers create the most muddled mess  you've ever seen. Some people just like to make the simple difficult.

Of course I use TT in Perl and Smarty in PHP so my progamming is just DATA MANIPULATION ... I get requests, I manipulate data and I return data structures which are then handled by TT or Smarty which SHOULD be handled by someone in charge of the presentation. In this way I use my code (especially in Perl) for both web, cron, cli applications, cus, need I say, I use MODULES and callers.

Oh, big, btw, I used mod_perl, MOD_PERL when I did Perl for web apps in a big way. Talk about power and control and yet ease of development.

Oh well. I could join the rest of my group in Java. I'm the legacy guy. Everyone else is moving forward and wasting a helluva lot of time not getting much done but they are using cool IDE's and personally I think I'd go crazy having to fit into the straight-jackets they are using.

me
Thursday, July 15, 2004

In IIS you can use Perl in ASP.

double_dark
Thursday, July 15, 2004

Ehm... OK! not really sure what your point was there, but still.

I dislike Perl for the usual kinds of reasons people dislike Perl, which I won't bore you with here. I'm not a massive PHP fan either but I do find it simpler to work with than Perl, when writing simple database-driven web apps. Just a matter of personal preference really. If I had my way I'd use Python all the time, but sadly it's rarely an option.

Matt
Thursday, July 15, 2004

And for reference, I do use heredocs in PHP when appropriate, and I don't use IDEs for this kind of thing, just a good text editor.

I agree that Perl is quite possibly more flexible/agile than PHP, especially if you like using regexps for everything, but the language as a whole just isn't to my taste. For web development PHP does the job and makes it relatively easy, but for more elegance power and flexibility Python all the way :)

Matt
Thursday, July 15, 2004

Cumbersome, is always the word for the first app/script in any language.

MyNameIsSecret();
Thursday, July 15, 2004

Matt,

The whole expense of starting a new process for each CGI instance is largely a myth.  Technically you do have to pay that price, but at least on modern UNIX that price is very small.  I put PHP (using mod_php4) head to head against a CGI program written in C, both performing the exact same function.  If the processing was very simple, PHP and C were identical in speed, although the system load was about half for the C program as compared to the PHP program.  If the processing was complex, say involving a template engine, the C program won.

I didn't do extensive research into the cause of this, but my guess is that opening the file from disk is the most expensive operation.  Reading is fairly rapid, and process creation is very rapid.

The conclusion that I came to was that while PHP is really excellent for rapid development, if the application is heavily used the compiled application is probably a better choice.  The aforementioned FastCGI would probably be worth looking at too.

Six Apart (http://www.sixapart.com) is making decent money writing Perl CGI applications, so it isn't completely dead.  The accounting package I use, SQL Ledger, is also written as Perl CGI.

Clay Dowling
Thursday, July 15, 2004

Perl is a great choice for creating web applications.  You don't have to use strings to build up the HTML and then print it.  There are a lot of HTML/XML templating "engines" out there to help you with that.  There are also quite a few projects that allow you to intermingle perl code with HTML, just like PHP.  Although, I wouldn't recommend going that route.  Just because PHP LETS you intermingle your code with the presentation layer (html), doesn't mean you should.

saberworks
Thursday, July 15, 2004

saber-

I had the same attitude but I've found some reasonable success intermingling php with presentation code in order to build my templates.  IE, instead of creating templates with a made-up markup language for invoking my objects and such, I just use simple PHP code blocks.  It saves development effort and hence, time (and sanity).

When I want to change my presentation, I just write a brand new template and use the same PHP code fragments as my markup "tags", and the backend functions just the same, with a totally different front end layout and style.

It works well for me, but perhaps I'm not explaining well.  There's no real LOGIC going on in my template, just brief and easy-to-understand (due to naming conventions) method calls to invoke particular content at point X in my template.

muppet
Thursday, July 15, 2004

Perl would incur more overhead then C cgi, but I tend to agree that PHP much more scalable than cgi as it is touted.  Amazon, Cisco and a  bunch of others use cgi or variations thereof.

.net, the equivalent of MS Bob.
Thursday, July 15, 2004

should read php is not as much more scalable than it is being touted

.net, the equivalent of MS Bob.
Thursday, July 15, 2004

mod_perl seems to beat the pants off of PHP for performance (and versatility, too), but setting up your framework is a considerable effort.

muppet
Thursday, July 15, 2004

Clay - I wouldn't be too surprised that something written in C is faster than PHP, even as a CGI app. To make a fair comparison you should compare the C compiled CGI binary against a C compiled apache extension, or compare PHP run using mod_php4 against PHP run with CGI.

Matt
Thursday, July 15, 2004

I love Perl. I've only seen the smallest snippets of PHP. Perl does everything I need it to do, so why change?

I never did get into the FastCGI stuff prefering to write my own template engine etc, but that's just me. It may not be pretty, it may not be what you'd do, but it works, and works well.

Jack of all
Thursday, July 15, 2004

Ever heard of PerlEx? It claims to improve Perl's performance on webservers... I've never really tried it, but it might be of interest to you.

irc
Friday, July 16, 2004

http://www.twiki.org is a fairly large Perl CGI application. And I prefer it to all the PHP Wiki's I've seen.

MugsGame
Friday, July 16, 2004

First of all a note to anon: Perl has similar code-embedded-in-HTML technologies to PHP/ASP/JSP. Google or search CPAN for HTML::Mason, Apache::ASP, Embperl, ePerl, HTML::Merge. If that's how you like to write your web applications you perfectly can. That's not a problem.

As for the original question, there is quite a lot of web-scripting going on in Perl nowadays. Perhaps not as much, or not relatively as much as what used to be, due to competition from PHP, Python, Java, etc. but there still is.

In fact, I like to write my web applications in Perl. They can run fast if you're using mod_perl, and there are plenty of useful modules and frameworks available on CPAN. It's also cross-platform, open-source, very powerful, has a nice and active culture behind it, and very fun to program in.

Note that it is possible that other similar technologies have some or all of the advantages of the above.

BTW, there are several concentrated links to critiques of PHP here:

http://czth.net/pH/PHPSucks

(in regards to what Matt said).

Shlomi Fish
Friday, July 16, 2004

*  Recent Topics

*  Fog Creek Home