Vol. 2, No. 8
More Delicious Fun with CGI.pm
Last time, as you recall, we -- what? You don't remember? Not a
word? Boy oh boy, you're slipping. You used to be so sharp. Okay,
fine, here's the link.
All caught up? Okay then. In our last session, we discussed CGI.pm,
the fantastic CGI library for Perl, and how to use it to generate HTML
pages from a CGI script. The flip side of that process is taking data
that's submitted via an HTML for and using a CGI script to parse and
deal with it. That, as it happens, is another thing that our beloved
CGI.pm makes pie-easy.
You all know how to create an HTML form that calls a CGI script when
it's submitted. The key is the FORM tag: you want to specify a METHOD,
either "GET" or "POST"; and an ACTION, which in our case is just the
URL of the CGI script. Therefore:You all know how to create an HTML form that calls a CGI script when
it's submitted. The key is the FORM tag: you want to specify a METHOD,
either "GET" or "POST"; and an ACTION, which in our case is just the
URL of the CGI script. Therefore:
Setting forms aside for today, let's take a look at the page-creation
functions of the library. Say you want a very simple page with just a
single sentence on it. In HTML, you have to go through all this:
<FORM METHOD="POST" ACTION="../cgi-bin/script.cgi">
<INPUT TYPE="TEXT" NAME="textbox><BR>
<INPUT TYPE="SUBMIT" NAME="sub_button" VALUE="Submit">
</FORM>
That gives us a little form with a text box and a Submit button that,
when clicked, calls a CGI script called script.cgi. Questions? Didn't
think so. Just for fun, let's take a look at how to use CGI.pm to
generate that same form, using the skills we mastered last time:
print startform(-method=>'POST',-action=>'../cgi-bin/script.cgi');
print textfield(-name=>'field_name');
print submit(-name=>'sub_button',-value=>'Submit');
print endform;
In this case we aren't saving a whole bunch of labor by typing in
the latter rather than the former; it's a bit more wear on the old
fingertips, in fact. But the great advantage (as you remember) of
using Perl to generate our form is that all of those specific tag
attributes can be replaced by variables, to generate a different
form each time the script is run, depending on your needs and your
users' input.
Now, where were we? Ah yes, parsing form input. The first step is
to grab the form input. Form input typically comes in two flavors,
depending on which method the form uses: GET or POST. If the GET
method is used, the data submitted by the form is just appended to
the URL of the script called by the ACTION attribute of the form,
after a question mark. It typically takes the form of name/value
pairs separated by ampersands. So if you fill out a GET-method-using
form that asks for your name, age, and favorite animal, and then
click Submit, something like this might appear in your browser's
location bar..
Everything before the question mark acts like a standard URL --
telling the browser what page to load. The stuff after the question
mark just sits there, waiting to be picked up by the script. (If
there is a script. If you're just loading an ordinary HTML page, you
can add on a question mark and whatever you want after the URL, and
nothing untoward will happen to you.)
The other method is the POST method, which passes the same data, but
keeps it out of the URL string. By and large, CGI.pm doesn't much care
which method you use.
Either way, you've sent data from a form to a CGI script. Now how does
thescript get its (metaphorical) hands on that data? The new() method
grabs all the data as a CGI object and puts it in a single variable
for you to work with.
use CGI qw/:standard/;
$data = new CGI;
Now that's something we can work with. The param() method can be
used to parse a data string, to access individual name/value
parameters. To pull out the value of, say, the "age" parameter from
the query string we saw above, just ask param() -- passing it the
name of the named parameter whose value you want:
$age = $data->param('age');
and voila! The variable $age now equals 12.
To import all of the data-string's parameters' names into an array,
param() can be invoked without any arguments, and sent to an
array-type variable (which is indicated by an initial @). So:
@allnames = $data->param;
The names are listed in the array in the order that they're
originally passed along by the form.
Putting together what we've learned, we can piece together a simple
(if useless) script like this:
use CGI qw/:standard/;
print header;
print start_html;
print param('name')," is ",param('age')," years old. ",param('name'),"\'s
favorite animal is the ",param('animal'),".";
print end_html;
That will generate an HTML page based on the user's input. In the
case of our example data, the page will say "Alice is 12 years old.
Alice's favorite animal is the giraffe."
There's literally a ton more that can be done with CGI.pm, from
cookies to CSS to saving state to a file. That all can wait for
future installments. Now you know how to generate HTML code and
forms, and how to parse CGI input. That's enough to keep you busy
for a long, long time.
RESOURCES:
Intro to CGI
All about CGI.pm
Tripod's Script Editor
Webmonkey's Guide to CGI
Instructions for working with CGI.pm