Perl & HTML Generation

There are probably vast amounts of documentation and tutorials about the Perl CGI Module but readers of this site who are not Perl programmers probably won't go out on a whim and read about this interesting module. Even a PHP programmer might get inspired ... is a "Simple Common Gateway Interface Class." I'm still learning myself how to use it, so I'll share what I know..

CGI can be used with two types of programming styles, functional and OOP. So far I've only used the functional.

Include the module with

use CGI qw/:standard/;

Here I'm bringing the "standard" set of functions into my scope. From my limited understanding thus far, the qw thingy delcares what to "export" into the local scope. Anyways, thats what I'm going on now until I become reach monkhood and earn my Camel Pin.

As always when learning something new, I start Uber Simple. Inspired by a exercise in Practical Perl Programing, I created a form that will take hourly rate, hours, overtime and spit out the salary.

print header;


Content-Type: text/html; charset=ISO-8859-1

Unlike with PHP (which I think defaults to text/html) you need to tell the server whats going on. "header" is a function that will generate the http header for your document.

HTML elements are defined as function names with their element...

sub print_header {
        print header,
              start_html('Salary Calculator'),
              h3('Handy Dandy Salary Calulator');
sub print_footer {
        print end_html;

Note that like PHP, the print statement can take multiple elements with the comma.

For my simple application, I created a function that will output the http header, call start_html which creates the beginning html and title tag with the title of the page, and display title on the page with the h3 tag. For completion sake, I also created a print_footer function.

Now, to create the form

sub print_form {
    print start_form;
    print "Rate: ", textfield('rate'),p;
    print "Hours: ", textfield('hours'),p;
    print "Overtime Hours: ", textfield('overtime_hours'),p;
    print submit({-name=>'btnCalculate',-value=>'Calculate'});
    print end_form;

Pretty self-explanatory. There's a function to start the form, generate textfields, submit button and then end the form. And of course, don't forget to notice the p functions to do a paragraph tag. The submit is interesting, you can pass it a hash of parameters, as with any of the functions. Say I wanted to add an “id” to my text field, as I am a good programmer who test this later. I could do this:

print "Hours: ", textfield({-name=>'hours',-id=>'hoursID'}),p;

Using the named parameters is more appealing to me since I don't need to remember the order of arguments. The docs say that using curly braces is actually optional in all but the html shortcuts, so I wouldn't really need them in the submit function, but I think it looks better.

Now I've written functions in PHP to generate HTML. Here's one that should generate a good laugh:

I made it very early in my PHP days, before I knew about passing associative arrays for long (I say more than 3) parameters and before I knew about PHP's default parameters.

function textField($fieldName) {
   $aArgs = func_get_args();
   $fieldName = "name=\"$fieldName\"";
   $fieldLength = ($aArgs[1] != "") ? "size=\"$aArgs[1]\"" : "";
   $fieldValue  = ($aArgs[2] != "") ? "value=\"$aArgs[2]\"" : "";
   $addParams   = ($aArgs[3] != "") ? "$aArgs[3]" : "";
   return ("<input type=\"text\" $fieldName $fieldValue $fieldLength $addParams>\n");

Only a sick person laughs at his or her own bad code...and shares with the world.

If I wrote it today it might look like this:

function textField($params) {
   $html = "<input ";
   foreach($params as $attr => $value) {
       $html .= "$attr='$value' ";
   $html .= ">";

Thats if I didn't care to do any attribute checking.
Now here's the thing ... what is better .. generating html with code.. or using templates? Early in my PHP days, I made functions to generate HTML. Then I used objects (It was verbose enough to make even Java programmers feel at home). Now, my general feeling on the subject is to use templates.

What do you think? please take a moment and vote on the poll