Jump to content

CGI.pm: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Markstos (talk | contribs)
List Mark Stosberg as the current developer/maintainer.
m Examples: Replace CPAN links with Metacpan ones
Line 18: Line 18:
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| genre = [[Perl module]] for [[Common Gateway Interface|CGI]]
| license =
| license =
| website = {{URL|http://search.cpan.org/dist/CGI/}}
| website = {{URL|https://metacpan.org/release/CGI}}
}}
}}
'''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 user input and producing [[HTML]] or [[XHTML]] output.
'''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 user input and producing [[HTML]] or [[XHTML]] output.

Revision as of 08:42, 15 September 2013

CGI.pm
Original author(s)Lincoln Stein
Developer(s)Mark Stosberg
Stable release
3.49 / 2010-02-05
PlatformPerl
TypePerl module for CGI
Websitemetacpan.org/release/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 was written by Lincoln Stein and is now maintained by Mark Stosberg.

Examples

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

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 interface:

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;

See also