Sat 28 Oct 2006
There is an old saying amongst carpenters. Measure twice, cut once. This cuts down on materials. The same is true of typing for Perl programmers. Read more, type less. Okay, that doesn't have the same ring, but reading the documentation before writing your script does make sense.
I came across a script recently where a beginner programmer had obviously decided that typing was easier than reading. Here's an excerpt. The author needs to start a really wide table, but won't end it for a while yet.
If we pretty this up some, the code above produces the following output. Hardly legible mark up.
Some experienced readers can spot several mistakes here. The first one is that CGI.pm provides a routine to start a table separately from ending it. Adding *table imports the start_table and end_table routines.
The next mistake shows a lack of reading (or understanding) about how the th() and td() routines work and how xhtml tables are built. Basically, arguments passed to these subs are incorporated into the table cell. I would venture that the programmer would have rathered this mark up.
Here's the code to do it.
If any of this looks strange, you know what you need to do: Go read the documentation. For now though, I can give lost readers a grasp of what is happening here. In its markup subroutines, CGI.pm allows markup atttributes to be defined by passing an anonymous hash before other arguments (if any).
The table routine in the example defines a border attribute with a value of 1. A more typical use might be an anchor.
-
<a href="http://ManThisAuthorIsGood.com/">Charles Clarkson</a>
There's not a whole lot to remember. We want an anchor, so the subroutine name is a() and the attribute is href. We place the attribute name in an anonymous hash and the text in quotes after the hash. We separate the arguments with a comma.
-
a( { href => 'http://ManThisAuthorIsGood.com/' }, 'Charles Clarkson' )
Another common feature of CGI.pm markup sub routines is their distributive nature. They allow us to replace this code.
With this code.
To make this program more robust, we might use a column name array.
-
my @column_names = (
-
'day', 'Date', 'Time In',
-
'Break Start', 'Break End', 'Time Out',
-
'Evening In', 'Evening Out', 'flex hours',
-
'flex bal', 'lieu', 'adjustment',
-
'lieu adjustment', 'type',
-
);
-
start_table( { border => 1 } ),
-
Tr( th( \@column_names ) );
Leave a Reply
You must be logged in to post a comment.