next up previous contents
Next: A Minimal Form Response Up: CGI Script Input: Accepting Previous: cgi-lib.pl

The cgi.pm module

Details of this module may be found at

http://stein.cshl.org/WWW/software/CGI/cgi_docs.html

This perl 5 library uses objects to create Web fill-out forms on the fly and to parse their contents. It provides a simple interface for parsing and interpreting query strings passed to CGI scripts. However, it also offers a rich set of functions for creating fill-out forms. Instead of remembering the syntax for HTML form elements, you just make a series of perl function calls. An important fringe benefit of this is that the value of the previous query is used to initialize the form, so that the state of the form is preserved from invocation to invocation.

Everything is done through a ``CGI'' object. When you create one of these objects it examines the environment for a query string, parses it, and stores the results. You can then ask the CGI object to return or modify the query values. CGI objects handle POST and GET methods correctly, and correctly distinguish between scripts called from <ISINDEX> documents and form-based documents. In fact you can debug your script from the command line without worrying about setting up environment variables.

A script to create a fill-out (cgipm.pl) form that remembers its state each time it's invoked is very easy to write with CGI.pm:

#!/usr/local/bin/perl

use CGI qw(:standard);

print header;
print start_html('A Simple Example'),
    h1('A Simple Example'),
    start_form,
    "What's your name? ",textfield('name'),
    p,
    "What's the combination?",
    p,
    checkbox_group(-name=>'words',
                   -values=>['eenie','meenie','minie','moe'],
                   -defaults=>['eenie','minie']),
    p,
    "What's your favorite color? ",
    popup_menu(-name=>'color',
               -values=>['red','green','blue','chartreuse']),
    p,
    submit,
    end_form,
    hr;

if (param()) {
    print 
        "Your name is",em(param('name')),
        p,
        "The keywords are: ",em(join(", ",param('words'))),
        p,
        "Your favorite color is ",em(param('color')),
        hr;
}
print end_html;

However for the ramainder of this course we will not use cgi.pm as we will concentrate on CGI aspects that would get clouded with the employemnt of the large module.

We keep things simple for easier understanding.

NOTE HOWEVER

Real CGI Perl programmer always use this module. If this is your game you should take time out to leran and harness the routines available here as they will save you mush effort in the future.

Using cgi-lib.pl is not advised with objetc oriented perl 5. Infact if you use the strict pragma or evenet the -w option you will get warning errors for good code as cgi-lib.pl contains some poor perl.

But it has a good purpose thats saves us some effort when learning some simple Perl CGI.

Lets now look at a minimal Perl program to read a CGI script.


next up previous contents
Next: A Minimal Form Response Up: CGI Script Input: Accepting Previous: cgi-lib.pl
dave@cs.cf.ac.uk