Perl - CGI.pm module: Object Oriented CGI package


This is an overview of the Perl CGI.pm package and its use in CGI programming. You should have read before my Perl - Object-Oriented Programming Concepts tutorial. CGI.pm is a more powerful library but it requires Perl 5 and is part of the object-oriented Perl libraries. Example 17 shows the form-decode script we seen previously using CGI.pm. You can also see it working here. For a comparison of cgi-lib.pl and CGI.pm see this page.

Basics

Basic Methods

These are the methods corresponding to the functions in cgi-lib.pl, see table bellow:

cgi-lib.pl description CGI.pm syntax example

($q is instance)

PrintHeader generates the required header for a server reply header ( ) header(content type,status,headers) print $q->header; or

print header;

ReadParse decode form information and place results in %in and @in ReadParse( ) ReadParse

(only %in)

ReadParse;
param ( ) @name= param or



param("key1","key2", ...);

$meal = $q->param('order'); or

$meal = param('order');

MethGet returns true if method is GET, false if POST request_method ( ) request_method if (request_method) { .........}
HtmlTop initial HTML tags of a page. You can specify the TITLE: &HtmlTop(This) start_html ( ) start_html (-title =>"value",-atribute=> "value", ... print start_html (-title=>"Example of CGI.pm",-BGCOLOR=>"white");
HtmlBot returns the closing HTML tags of a page. end_html ( ) end_html print end_html;
MyFullUrl returns the full URL of the script self_url self_url print self_url;

Other Methods

There are too many methods to be covered in detail here. The best reference on these methods is maintained by the module author. Basic HTML methods are explained. Methods for creating forms are also available. Methods to access environment variables also exist. Support for cookies and frames are also included, along with many others. CGI.pm works with UNIX, Linux, NT, etc.


This page is maintained by Al Bento who can be reached at abento@ubmail.ubalt.edu. This page was last updated on October 7, 2002. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.