From Wikipedia, the free encyclopedia
Jump to: navigation, search
Original author(s) Lincoln Stein
Developer(s) Lee Johnson
Stable release 3.49 / 2010-02-05
Platform Perl
Type Perl module for CGI
Website 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 Lee Johnson.


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

use CGI;
my $cgi = CGI->new();
    $cgi->start_html('A Simple CGI Page'),
    $cgi->h1('A Simple CGI Page'),
    'Name: ',
    $cgi->textfield('name'), $cgi->br,
    'Age: ',
    $cgi->textfield('age'), $cgi->p,
    $cgi->end_form, $cgi->p,
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'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's procedural interface:

use CGI ':standard';
print header,
    start_html('A Simple CGI Page'),
    h1('A Simple CGI Page'),
    'Name: ',
    textfield('name'), br,
    'Age: ',
    textfield('age'), p,
    end_form, p,
print 'Your name is ', param('name'), br if param 'name';
print 'You are ', param('age'), ' years old.' if param 'age';
print end_html;

An astuce to write CSS perls' variables in chevrons (<<)

my($txt_Title) = 'INPUT_TITLE';
my($txt_content) = 'INPUT_CONTENT';
my($default_textSize) = '2.1';
/*	1 - the way to interdict perl to interpret a CSS variable. */
	\@keyframes mymove2 {
		50% {transform:rotate(5deg);}
/*	2 - to replace le value of $txt_titre HTML ID for CSS ID
	  - This one gives : #INPUT_TITLE {
*/	\#$txt_Title { /*mean: #INPUT_TITLE. !!! note the space between "Title" and "{" */
/*	3 - Idem to interpret a perl value into CSS Class variable.
	  - This one gives : .INPUT_CONTENT {
*/	\.$txt_content { 
		font-size: $default_textSize\pt;
		/* hear perl take the content of $default_textSize, you must cut from "pt" with anti-slash */

the CSS variable can be include as it:

print $query->header(-type => 'text/html',

See also[edit]

External links[edit]