Skip

How do I make simple dynamic web pages using perl?
August 22, 2008 9:24 AM   Subscribe

How should I develop simple dynamic web pages using perl? Does anyone have any suggestions on how best to produce simple web pages in perl that execute logic (e.g., opening property documents to retrieve property values and saving back changes). I am new to perl and would like some detail instructions or links to tutorials. Would a framework like Mason be overkill?
posted by zzztimbo to Computers & Internet (6 answers total) 3 users marked this as a favorite
 
Yes, it will be overkill. Use the CGI module after reading the documentation carefully.
posted by msittig at 10:12 AM on August 22, 2008


For very simple pages, you can use plain perl with the CGI module, and embed the page HTML inside the perl script (just using "print" statements to output the html to the browser).

The next step up is to use one of the many template systems, to separate your HTML from your perl code. (The general procedure is to sort out all the data you need in the perl script, then spit it out as variables into a template; all the layout and UI decisions get done in the template instead of in perl. Much easier to maintain that way.)

I used to use HTML::Template, but eventually migrated to Template Toolkit which is slightly more complicated but a lot more powerful.

For simple pages, a full framework like Mason would definitely be overkill, but a template system is always a good idea. Program logic over here, layout over there. Never mingle the two.

(Also, if you haven't seen it yet, CPAN is your new best friend.)
posted by ook at 10:20 AM on August 22, 2008


(A quick get-up-and-running with Template Toolkit is here.)
posted by ook at 10:23 AM on August 22, 2008


I'd say a templating system is appropriate for dynamic web page generation nearly all the time. Like ook, I've recently migrated from HTML::Template to Template Toolkit, and have been pretty happy with the change.
posted by Zed_Lopez at 10:59 AM on August 22, 2008


For very simple pages, you can use plain perl with the CGI module, and embed the page HTML inside the perl script (just using "print" statements to output the html to the browser).

ook's quite right this is the simplest way to do things, and it's how a lot of us doing web development started out, but I recommend *not* doing it on anything, even simple projects, except test projects that will never see real use. There are two reasons:
  1. It's likely that anything useful, however small, will eventually be extended, becoming not quite so simple over time.
  2. Someday, it is likely you will build something bigger, even if it's not extending the particular small useful thing you're building. When that day comes, it's good to already have good habits.
So... starting at "the next step up" is generally a good idea. :)

If you *do* for whatever reason decide to go the absolute simplest route, rather than printing each piece of output, try keeping a running concatenation of the output in a buffer variable as you go along. That is, instead of:

print "\n<br>some piece of output";

do:

$output .= "\n<br>some piece of output";

This carries a bit of a performance hit, but it allows you to modify output as you go and switch to something else at any time. And if the performance hit is a big concern, then you need to be on the next step up. :)

a template system is always a good idea. Program logic over here, layout over there.

Very good advice. Template Toolkit is a good suggestion, too. I kindof like Petal as well.
posted by weston at 11:06 AM on August 22, 2008


I'll go against the grain a bit and say that you might want to use a framework. Some are pretty simple and they'll handle the storing and retrieving of user state information for you. (the whole retrieve values and save changes thing that everybody has missed so far).

If you're new, start visiting and searching Perl Monks. A wealth of information and help can be found there.
posted by zengargoyle at 12:43 PM on August 22, 2008


« Older Why are so many RBs ranked abo...   |  Babynamefilter: I need help f... Newer »
This thread is closed to new comments.


Post