Formatting HTML for CGI.pm
By Randal L. Schwartz
The CGI.pm module, created by fellow columnist Lincoln Stein, is the most commonly used method for dealing with all the ins, outs, and roundabouts regarding the common gateway interface. One of the nice features of this CGI module is that there are numerous HTML-creation subroutines. I personally find it easier to type
h1("Welcome to my Fred & Barney page!")
than it is to type
<h1>Welcome to my Fred & Barney page!</h1>
especially when the content or the parameters come from various variables. Now, if I'm constructing a new form generator and form response program from scratch, I just start typing the correct HTML-creation routines, and I'm done.
But let's say I had started with a static HTML form page, which had originally referenced a CGI program (perhaps something that uses the CGI module, but not necessarily), and now I want to roll it up into the "same program generates the form and handles the response" method. I've got all this nice HTML that should be the output of the HTML-creation routines. What I want are the routines that could have created that output.
Perl to the rescue! All we have to do is parse the HTML (easy enough with some of the existing LWP library components), and dump the parsed structure in a way that's compatible with the CGI module's routines.