Using mod CGI

What is it? A set of objects and methods (a class) that simplifies using CGI and creating HTML forms in Perl.

Now that we know something about creating WEB pages from Perl scripts, life is about to get seriously easier, because not surprisingly, someone has written a Perl module to simplify and structure the process.

Here's a link to documentation on mod CGI:

http://stein.cshl.org/WWW/software/CGI::modules/

A module made accessible to your program through the use command:

use CGI qw(:standard :html3);

To define a CGI object the syntax is:

$q = new CGI;

Once you have $q defined you can do all sorts of things simply by using the methods already defined for CGI objects: for example;

$remote_host = $q->remote_host();

extracts the IP address of the remote host.

Processing cookies.

Cookies are pulled off of the submitted form using:

$the_time = cookie('Time'); # The time the program was started.

They are created using the syntax

$Code_cookie = cookie(-name => 'Code', -value => \@Code);
$Time_cookie = cookie(-name => 'Time', -value => $the_time);

and are sent using the syntax

print $q->header(-cookie => [$Code_cookie,$Time_cookie]);

HTML pages are started with

print $q->start_html;

and ended with

print $q->end_html;

So far we have only used very simple form inputs. HTML forms provide for a variety of inputs, such as check boxes, text fields, radio buttons, selectiosn and multiple selections. We'll look at my example to see these.

Forms are started with

print $q->start_form;

and ended with

print $q->end_form;

There are various predefined methods for generating the various inputs; see documentation and my proto-example.


next up previous
Next: Mastermind program Up: Class 3 Stat 540 Previous: Saving state

Richard Waterman
Fri Jan 29 06:45:16 EST 1999