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.
You can also invoke CGI.pm specifying that you want to import its methods:
The advantage of doing so is that you no longer need to instantiate the CGI object, CGI.pm will do it for you. You also no longer need to use the CGI module reference. In this example you see how this simplifies the coding:
You can compare with doing without a CGI library, and using cgi-lib.pl. If you import the CGI.pm methods and do not use the -> notation you could write the script as:
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; |
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.