Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
CGI.pm
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Perl module for web applications}} {{primary sources|date=September 2011}} {{one source |date=April 2024}} {{Infobox software | name = CGI.pm | logo = | screenshot = | caption = | author = [[Lincoln Stein]] | developer = Lee Johnson | released = | latest release version = 4.21 | latest release date = 2015-06-22 | latest preview version = | latest preview date = | operating system = | platform = [[Perl]] | language = | genre = [[Perl module]] for [[Common Gateway Interface|CGI]] | license = | website = {{URL|https://metacpan.org/release/CGI}} }} '''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 name="auto">{{Cite web|url=https://metacpan.org/dist/CGI/view/lib/CGI.pod|title=CGI - Handle Common Gateway Interface requests and responses - metacpan.org|website=metacpan.org}}</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. == Examples == Here is a simple CGI page, written in Perl using CGI.pm (in [[Object-oriented programming|object-oriented]] style): <syntaxhighlight lang="perl"> #!/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>'; </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 {{mono|$cgi->}}, however the necessary functions must be imported into the namespace of the script that requires access to those functions: <syntaxhighlight lang="perl"> #!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>'; </syntaxhighlight> Note: in many examples {{var|$q}}, short for query, is used to store a CGI object. ==See also== *[[mod_perl]] ==References== {{Reflist}} ==External links== {{Wikibooks|Perl Programming|CGI}} *[https://metacpan.org/module/CGI CGI.pm] – at the [[CPAN]] [[Category:Perl modules]] {{unix-stub}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Infobox
(
edit
)
Template:Infobox software
(
edit
)
Template:Main other
(
edit
)
Template:Mono
(
edit
)
Template:One source
(
edit
)
Template:Primary sources
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Template other
(
edit
)
Template:Unix-stub
(
edit
)
Template:Var
(
edit
)
Template:Wikibooks
(
edit
)