Learning Perl

Learning PerlSearch this book
Previous: 19.3 Simplest CGI ProgramChapter 19
CGI Programming
Next: 19.5 Less Typing

19.4 Passing Parameters via CGI

You don't need a form to pass a parameter to (most) CGI programs. To test this, change the URL to http://www.SOMEWHERE.org /cgi-bin/ice_cream?flavor=mint

When you point your browser at this URL, the browser not only requests the web server to invoke the ice_cream program, but it also passes the string flavor=mint to the program. Now it's up to the program to read the argument string and pick it apart. Doing this properly is not as easy as you might think. Many programs try to wing it and parse the request on their own, but most hand-rolled algorithms only work some of the time. Given how hard it is to get it right in all cases, you probably shouldn't try to write your own code, especially when perfectly fine modules already handle the tricky parsing business for you.

Enter the CGI.pm module, which always parses the incoming CGI request correctly. To pull this module into your program, merely say

use CGI;

somewhere near the top of your program.[5]

[5] All Perl modules end in the suffix ".pm"; in fact, the use statement assumes this suffix. You can learn how to build your own modules in Chapter 5 of Programming Perl or the perlmod (1) manpage.

The use statement is like an #include statement in C programming in that it pulls in code from another file at compile-time. But it also allows optional arguments specifying which functions and variables you'd like to access from that module. Put those in a list following the module name in the use statement. You can then access the named functions and variables as if they were your own.

In this case, all we need to use from CGI.pm is the param() function.[6]

[6] Some modules automatically export all their functions, but because CGI.pm is really an object module masquerading as a traditional module, we have to ask for its functions explicitly.

If given no arguments, param() returns a list of all the fields that were in the HTML form this CGI script is responding to. (In the current example that's the flavor field. In general, it's the list of all the names in name=value strings received from the submitted form.) If given an argument naming a field, param() returns the value (or values) associated with that field. Therefore, param("flavor") returns "mint", because we passed in ?flavor=mint at the end of the URL.

Even though we have only one item in our import list for use, we'll employ the qw() notation. This way it will be easier to expand the list later.

#!/usr/bin/perl -w
# cgi-bin/ice_cream: program to answer ice cream
# favorite flavor form (version 1)
use CGI qw(param);

print <<END_of_Start;
Content-type: text/html

    <TITLE>Hello World</TITLE>
    <H1>Greetings, Terrans!</H1>

my $favorite = param("flavor");
print "<P>Your favorite flavor is $favorite.";
print <<All_Done;

Previous: 19.3 Simplest CGI ProgramLearning PerlNext: 19.5 Less Typing
19.3 Simplest CGI ProgramBook Index19.5 Less Typing