CGI.pm

Chapter 7 - Great CPAN Modules
by?Sam Tregar?
Apress ? 2002
has companion web siteCompanion Web Site

The CGI module, often written "CGI.pm" to distinguish it from the standard it supports, is the granddaddy of successful CPAN modules.[3] Perl is the most popular environment for CGI programming,[4] and CGI.pm is the most popular way to go about it. CGI.pm was created by Lincoln Stein[5] as a Perl5 implementation of the facilities provided by the venerable Perl4 library cgi-lib.pl. It leveraged the new OO support in Perl 5 to provide a simpler way to write CGI scripts. CGI.pm also included many new features, particularly extensive support for HTML generation.

The most obvious reason CGI.pm is so popular is that it represents a complete solution to a difficult problem. Nearly every capability you could want in a CGI environment is present, from parameter parsing to file uploads to cookie handling to HTML form generation. All of these capabilities are documented clearly with full examples of nearly every possible usage.

Another highlight is the interface design. Lincoln Stein has embraced TMTOWTDI[6] to a degree that few have managed. For example, a critical task in CGI is parameter parsing and processing. CGI.pm provides five different ways to access the CGI parameters. Many of these are supplied to allow for backwards compatibility with older libraries and older versions of CGI.pm. Stein has always focused on keeping older scripts working even as he enhances CGI.pm with new features.

Furthermore, CGI.pm provides its functionality through both object-oriented and functional interfaces. This is accomplished by starting each and every subroutine in CGI.pm as follows:

 sub method_foo {    my($self,@p) = self_or_default(@_); 

The self_or_default() routine extracts $self from @_, or if $self is not found uses a globally defined CGI object instead. This means that users can pick either an object-oriented style:

 use CGI; my $cgi = CGI->new; print join "\n", $cgi->param(); 

or functional calling style:

 use CGI qw(param); print join "\n", param(); 

People writing quick scripts can benefit from the brevity of the functional interface, whereas those developing more intensive CGI code can benefit from the extensibility of the OO interface.

For all that is great about the CGI module, it has its detractors. The usual complaints fall along the lines of memory consumption, performance and sheer code size. For these reasons there are many competitors to CGI.pm on CPAN—CGI::Lite,[7] CGI::Minimal,[8] and CGI::Thin,[9] to name a few. These modules tend to start by implementing just the core CGI functionality and grow slowly from there as the author realizes that this or that CGI.pm function is really quite useful after all. Ultimately they may perform better and use less memory, but CGI.pm remains more popular for a good reason—it does everything a CGI programmer needs and it does it well.

[3]So successful, in fact, that it's now included with Perl! But it was once a CPAN module, so I feel justified including it here.

[4]Otherwise known as Web programming. CGI, or Common Gateway Interface, is used by programmers to create interactive applications for the Web.

[5]See Lincoln Stein's site at http://www.stein.cshl.org/.

[6]Short for "There's more than one way to do it."

[7]Written by Benjamin Low

[8]Written by Benjamin Franz

[9]Written by R. Geoffrey Avery



Writing Perl Modules for CPAN
Writing Perl Modules for CPAN
ISBN: 159059018X
EAN: 2147483647
Year: 2002
Pages: 110
Authors: Sam Tregar

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net