CGI.pm
Encyclopedia
CGI.pm is a large and widely used Perl module
Perl module
A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl's package mechanism that has become universally adopted....

 for programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

 Common Gateway Interface
Common Gateway Interface
The Common Gateway Interface is a standard method for web servers software to delegate the generation of web pages to executable files...

 (CGI) web applications, providing a consistent API
Application programming interface
An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

 for receiving user input and producing HTML
HTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....

 or XHTML
XHTML
XHTML is a family of XML markup languages that mirror or extend versions of the widely-used Hypertext Markup Language , the language in which web pages are written....

 output.

The module was written Lincoln Stein
Lincoln Stein
Professor Lincoln D. Stein is a scientist and leader in bioinformatics and computational biology at the Ontario Institute for Cancer Research. Current projects include Reactome, WormBase,, BioPerl, , ENCODE , the Generic Model Organism Database and Cloud computing...

 and is now maintained by Mark Stosberg.

A Sample CGI Page

Here is a simple CGI page, written in Perl using CGI.pm (in object oriented
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 style):

  1. !/usr/bin/perl


use strict;
use warnings;
use CGI;

my $cgi = CGI->new;

print
$cgi->header('text/html'),
$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.

Here is another script that produces the same output using CGI.pm's procedural
Procedural programming
Procedural programming can sometimes be used as a synonym for imperative programming , but can also refer to a programming paradigm, derived from structured programming, based upon the concept of the procedure call...

 interface:

  1. !/usr/bin/perl

use strict;
use warnings;
use CGI ':standard';

print header,
start_html('A Simple CGI Page'),
h1('A Simple CGI Page'),
start_form,
'Name: ',
textfield('name'), br,
'Age: ',
textfield('age'), p,
submit('Submit!'),
end_form, p,
hr;

print 'Your name is ', param('name'), br if param 'name';
print 'You are ', param('age'), ' years old.' if param 'age';

print end_html;

External links

  • Official homepage
  • CGI.pm – at the CPAN
    CPAN
    CPAN, the Comprehensive Perl Archive Network, is an archive of nearly 100,000 modules of software written in Perl, as well as documentation for it. It has a presence on the World Wide Web at and is mirrored worldwide at more than 200 locations...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK