Jump to content

CGI.pm: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
no longer popular
Line 15: Line 15:
| platform = [[Perl]]
| platform = [[Perl]]
| language =
| language =
| status =
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| license =
| license =
Line 21: Line 20:
}}
}}


'''CGI.pm''' is a large and widely used [[Perl module]] for [[Computer programming|programming]] [[Common Gateway Interface]] (CGI) [[WWW|web]] applications, providing a consistent [[Application programming interface|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>https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used</ref> CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.<ref>https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE</ref> The module was written by [[Lincoln Stein]] and is now maintained by Lee Johnson.
'''CGI.pm''' is a large and once widely used [[Perl module]] for [[Computer programming|programming]] [[Common Gateway Interface]] (CGI) [[WWW|web]] applications, providing a consistent [[Application programming interface|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>https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used</ref> CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.<ref>https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#CGI.pm-HAS-BEEN-REMOVED-FROM-THE-PERL-CORE</ref> The module was written by [[Lincoln Stein]] and is now maintained by Lee Johnson.


== Examples ==
== Examples ==

Revision as of 07:28, 28 June 2020

CGI.pm
Original author(s)Lincoln Stein
Developer(s)Lee Johnson
Stable release
4.21 / 2015-06-22
PlatformPerl
TypePerl module for CGI
Websitemetacpan.org/release/CGI

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.[1] CGI.pm was a core Perl module but has been removed as of v5.22 of Perl.[2] The module was written by Lincoln Stein and is now maintained by Lee Johnson.

Examples

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

#!/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>
        <h1>A Simple CGI Page</h1>
        <form method="post" enctype="multipart/form-data">
            Name: <input type="text" name="name"  /><br />
            Age: <input type="text" name="age"  /><p>
            <input type="submit" name="Submit!" value="Submit!" />
        </form>
        <hr />
EndOfHTML

if ( my $name = $cgi->param('name') ) {
    print "Your name is $name.<br />";
}

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

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

#!perl

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

print header('text/html');

# ... HTML output same as above example

if ( my $name = param('name') ) {
    print "Your name is $name.<br />";
}

if ( my $age = param('age') ) {
    print "You are $age years old.";
}

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

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

See also

References