CGI.pm, the Middleman
July 12, 1999
Well, we usually talk about eliminating the middleman when
it comes to most of life, but here let's consider welcoming
the middleman -- in this case, the CGI.pm Perl module. Of
course, we've been using various facets of the CGI module all
along. In Part 3 of "The Perl You Need to Know" we
used the CGI module to output on-the-fly forms.
In fact,
CGI.pm,
as it also likes to be called, provides methods for creating
HTML output without having to explicitly print HTML code.
Whether these methods are advantageous is a valid question,
but let's meet them first, and judge them later.
To start with a simple example, suppose we want to start the
output of an HTML page. Rather than muck around with
<HTML> and
<HEAD>
tags, we can rely on CGI.pm methods:
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
$cgiobject->use_named_parameters;
print $cgiobject->header;
#begin HTML page
print $cgiobject->
start_html(-title=>"Shiny Modern Web Page",
-BGCOLOR=>"white");
After creating an instance of the CGI object we call
use_named_parameters, which simply makes it easier
to name the parameters in creating HTML elements (we'll see
in a moment). We supply the CGI method start_html
with two named parameters: title and BGCOLOR.
Notice the syntax here ... each named parameter begins
with a dash and is assigned its value with =>. If you
forget the cap > on the equal sign the Perl script
will fail, a common source of error.
In sum, the single call to start_html creates output
equivalent to the hand-coded HTML:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML><HEAD><TITLE>Shiny Modern Web Page</TITLE>
</HEAD><BODY BGCOLOR="white">
The CGI.pm methods essentially offer a simplified way to
express HTML content without handling all of the code.
There is a method for most HTML2 and HTML3 tags, which
covers most that you'd use. Of course, remembering all these
methods is virtually impossible, and so you can consult the
CGI.pm HTML Shortcuts Reference
for a complete inventory.
We also saw in Part 3 of this series how these methods could
be used to create form fields which can otherwise
be hairy looking in pure HTML. Consider a series of
checkboxes, and compare the HTML output you'd have to code
versus the CGI.pm checkbox_group method.
Checkbox group created using CGI.pm
checkbox_group method.
print $cgiobject->
checkbox_group(-name=>'hobbies',
-values=>['reading','writing','music',
'sports','juggling','pottery',
'cooking','fishing','nagging',
'cat breeding','stargazing',
'navelgazing'],
-linebreak=>'false',
-columns=>4);
|
Checkbox group created using pure HTML.
print <<EOF
<TABLE>
<TR><TD>
<INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="reading">reading<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="sports">sports<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="cooking">cooking<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="cat breeding">cat breeding<BR></TD>
</TR>
<TR><TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="writing">writing<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="juggling">juggling<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="fishing">fishing<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="stargazing">stargazing<BR></TD>
</TR><TR><TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="music">music<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="pottery">pottery<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="nagging">nagging<BR></TD>
<TD><INPUT TYPE="checkbox"
NAME="hobbies"
VALUE="navelgazing">navelgazing<BR></TD>
</TR></TABLE>
EOF
|
It's clear that in this case, the CGI.pm is a preferable
means to generate this HTML content: it's easier to
read, edit, debug, and requires less typing. That said,
CGI.pm's HTML generation methods are not always this useful.
Many HTML tags are simple paired tags, meaning they open
and close around a bit of data, such as the <H3>
tag for a third-level heading. The coding difference between
the CGI.pm and pure HTML approaches is minor:
print $cgiobject->h3("Read all about it.");
vs.
print "<H3>Read all about it</H3>";
Of course, you are free to use CGI.pm to generate complex
portions of HTML and stick with pure HTML for the
shorter bits. For someone who is quite familiar with HTML,
coding simple passages in scratch may be no more or
less work than learning the CGI.pm method.
In any case, none of this addresses our outstanding problem
with generating on-the-fly Web pages which is that
you usually don't need to generate the entire page --
only the part that changes. Enter templates.
Code Like Grandma Used to Make
The Perl You Need to Know
Tantalizing Templates: Live Date
|