Using the CGI Module


Perl's CGI Module by Lincoln Stein can greatly simply the generating of html pages, both static and dynamic. It supports the generation of standard html text, the generation and interpertation of forms, and the use of cookies and frames.

Function-oriented usage

The CGI Module supports a limited set of functions using a function-based interface.
You specify which set of routines to import into your script using the following labels:

:cgi
:form
:html2
:html3
:netscape
:html
:standard        # this is the one we will use
:all

Here is a basic example for generating an html page:

use CGI qw(:standard);
print header;
print start_html('My Title');
print h1("My Heading");
print p("This is basically all I have to say.");
print end_html;

Output:

Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML><HEAD><TITLE>My Title</TITLE>
</HEAD><BODY><H1>My Heading</H1><P>This is basically all I have to say.</P></BODY></HTML>


Object-oriented usage

The most advanced features of the CGI module require using the object-oriented syntax. Here we repeat the above example using objects:
use CGI qw(:standard);
$page = new CGI;     # create new CGI Object
print $page->header,
      $page->start_html('My Title'),
      $page->h1("My Heading"),
      $page->p("This is basically all I have to say."),
      $page->end_html;




Tips for Securing Scripts