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 is written and maintained by Lincoln D. Stein.
use strict; use warnings; use CGI;
- !/usr/bin/perl
my $q = CGI->new();
print $q->header('text/html'); print $q->start_html('A Simple CGI Page'), $q->h1('A Simple CGI Page'), $q->start_form, 'Name: ', $q->textfield('name'), $q->br, 'Age: ', $q->textfield('age'), $q->p, $q->submit('Submit!'), $q->end_form, $q->p, $q->hr;
if ( $q->param('name') ) { print 'Your name is ', $q->param('name'), $q->br; }
if ( $q->param('age') ) { print 'You are ', $q->param('age'), ' years old.'; }
print $q->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 $q->.