Template:Short description Template:Primary sources Template:One source {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other

CGI.pm is a large and once widely used Perl module for programming Common Gateway Interface (CGI) web applications, providing a consistent API for receiving and processing user input. There are also functions for producing HTML or XHTML output, but these are now unmaintained and are to be avoided.<ref name="auto">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.<ref name="auto"/> The module was written by Lincoln Stein and is now maintained by Lee Johnson.

ExamplesEdit

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

<syntaxhighlight lang="perl">

  1. !/usr/bin/env perl

use strict; use warnings;

use CGI;

my $cgi = CGI->new;

print $cgi->header('text/html');

print << "EndOfHTML"; <!DOCTYPE html> <html>

   <head>
       <title>A Simple CGI Page</title>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   </head>
   <body>

A Simple CGI Page

       <form method="post" enctype="multipart/form-data">
           Name: <input type="text" name="name"  />

Age: <input type="text" name="age" />

<input type="submit" name="Submit!" value="Submit!" /> </form>


EndOfHTML

if ( my $name = $cgi->param('name') ) {

   print "Your name is $name.
";

}

if ( my $age = $cgi->param('age') ) {

   print "You are $age years old.";

}

print '</body></html>'; </syntaxhighlight>

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 Template:Mono, however the necessary functions must be imported into the namespace of the script that requires access to those functions:

<syntaxhighlight lang="perl">

  1. !perl

use strict; use warnings; use CGI qw/ :standard /;

print header('text/html');

  1. ... HTML output same as above example

if ( my $name = param('name') ) {

   print "Your name is $name.
";

}

if ( my $age = param('age') ) {

   print "You are $age years old.";

}

print '</body></html>'; </syntaxhighlight>

Note: in many examples Template:Var, short for query, is used to store a CGI object.

See alsoEdit

ReferencesEdit

Template:Reflist

External linksEdit

Template:Sister project


Template:Unix-stub