O'Reilly logo

Managing & Using MySQL, 2nd Edition by Hugh E. Williams, Randy Yarger, George Reese, Tim King

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Introduction to Perl CGI

Having been passed the titleword field by the web server, the Perl CGI module extracts it through a param( ) call:

my $titleword = $cgi->param('titleword');

Now the local variable $titleword contains the string Linux. The program can do whatever it wants with this information, but typically it makes a connection to a database and formulates an SQL query, as we did in the earlier DBI examples.

The CGI program is responsible both for handling user input and creating output. Since the server sends its output (more or less) directly to the browser, a Perl program can use simple print statements to create HTML. The program also has to create part or all of the HTTP header, but luckily the CGI module hides that activity behind the simple call:

print $cgi->header(  );

Other CGI calls are also fairly intuitive to people who know HTML. For instance, the statement:

print $cgi->h2("Please enter a word in the box above.");

outputs the string:

<h2>Please enter a word in the box above.</h2>

which is, of course, an H2 header element in HTML. Sometimes you have to specify arguments as hash keys and values. The following call, for instance, sets two of the many parameters accepted by the start_html( ) call: one parameter to set the background color and another to set the web page’s title:

$cgi->start_html(
                 -bgcolor => '#ffffff',
                 -title => 'MySQL CGI Example');

Parameters passed from the browser can easily be placed into SQL calls, and results retrieved from database calls can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required