[logo] 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

Contents

  • Downloading
  • Installation
  • Function-Oriented vs Object-Oriented Use
  • Creating a new CGI query object
  • Saving the state of the form
  • CGI Functions that Take Multiple Arguments
  • Creating the HTTP header
  • HTML shortcuts
  • Creating forms
  • Importing CGI methods
  • Retrieving CGI.pm errors
  • Debugging
  • HTTP session variables
  • HTTP Cookies
  • Support for frames
  • Support for JavaScript
  • Limited Support for Cascading Style Sheets
  • Using NPH Scripts
  • Advanced techniques
  • Subclassing CGI.pm
  • Using CGI.pm with mod_perl and FastCGI
  • Migrating from cgi-lib.pl
  • Using the File Upload Feature
  • Server Push
  • Avoiding Denial of Service Attacks
  • Using CGI.pm on non-Unix Platforms
  • The Relationship of CGI.pm to the CGI::* Modules
  • Distribution information
  • The CGI.pm Book
  • CGI.pm and the Year 2000 Problem
  • Bug Reporting and Support
  • What's new?

  • Downloads