This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources. Find sources: "CGI.pm" – news ·newspapers ·books ·scholar ·JSTOR(September 2011) (Learn how and when to remove this message) |
This articlerelies largely or entirely on asingle source. Relevant discussion may be found on thetalk page. Please helpimprove this article byintroducing citations to additional sources. Find sources: "CGI.pm" – news ·newspapers ·books ·scholar ·JSTOR(April 2024) |
Original author(s) | Lincoln Stein |
---|---|
Developer(s) | Lee Johnson |
Stable release | 4.21 / 2015-06-22 |
Platform | Perl |
Type | Perl module forCGI |
Website | metacpan |
CGI.pm is a large and once widely usedPerl module forprogrammingCommon Gateway Interface (CGI)web applications, providing a consistentAPI for receiving and processing user input. There are also functions for producingHTML orXHTML 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.[1] The module was written byLincoln Stein and is now maintained by Lee Johnson.
Here is a simple CGI page, written in Perl using CGI.pm (inobject-oriented style):
#!/usr/bin/env perlusestrict;usewarnings;useCGI;my$cgi=CGI->new;print$cgi->header('text/html');print<<"EndOfHTML";<!DOCTYPEhtml><html><head><title>ASimpleCGIPage</title><metahttp-equiv="Content-Type"content="text/html; charset=iso-8859-1"/></head><body><h1>ASimpleCGIPage</h1><formmethod="post"enctype="multipart/form-data">Name:<inputtype="text"name="name"/><br />Age:<inputtype="text"name="age"/><p><inputtype="submit"name="Submit!"value="Submit!"/></form><hr/>EndOfHTMLif(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:
#!perlusestrict;usewarnings;useCGIqw/ :standard /;printheader('text/html');# ... HTML output same as above exampleif(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.