Jump to content

CGI.pm

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 87.109.5.85 (talk) at 18:03, 16 November 2007 (<source lang="perl"></source>). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

CGI.pm
Original author(s)Lincoln D. Stein
Stable release
3.29 / 2007-04-16
PlatformPerl
TypePerl module for CGI
Websitehttp://stein.cshl.org/WWW/software/CGI/

CGI.pm is a large and widely used Perl module for programming Common Gateway Interface (CGI) web applications, providing a consistent API for receiving user input and producing HTML or XHTML output.

The module is written and maintained by Lincoln D. Stein.

A Sample CGI Page

Here is a simple CGI page, written in Perl using CGI.pm (in object oriented style):

#!/usr/bin/perl
#
use strict;
use warnings;
use CGI;

my $cgi = CGI->new();

print $cgi->header('text/html');
print $cgi->start_html('A Simple CGI Page'),
$cgi->h1('A Simple CGI Page'),
$cgi->start_form,
'Name: ',
$cgi->textfield('name'), $cgi->br,
'Age: ',
$cgi->textfield('age'), $cgi->p,
$cgi->submit('Submit!'),
$cgi->end_form, $cgi->p,
$cgi->hr;

if ( $cgi->param('name') ) {
    print 'Your name is ', $cgi->param('name'), $cgi->br;
}

if ( $cgi->param('age') ) {
    print 'You are ', $cgi->param('age'), ' years old.';
}

print $cgi->end_html;

This would print a very simple webform, asking for your name and age, and after having been submitted, redisplaying the form with the name and age displayed below it. This sample makes use of CGI.pm's object-oriented abilities; it can also be done by calling functions directly, without the $cgi->.

Note: in many examples $q, short for query, is used to store a CGI object. As the above example illustrates, this might be very misleading.