CGI.pm - a Perl5 CGI Library
Version 3.05, 04/12/2004, L. Stein
Abstract
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 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;
Select this link to try the script
More scripting examples
Source code
examples from The Official Guide to CGI.pm