Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Tips for Convenient CGI Scripting

By Eugene Logvinov
2005-04-28


The CGI module

The first point to be made in espousing the benefits of using the CGI module is that it is irreplaceable for the file upload function, which differs slightly in various browsers and platforms. The CGI module provides all the tools needed to upload files, including those for handling HTTP headers and cookies, running scripts from the command line, and supporting NPH scripts and the file upload function. And, conveniently, the module can be used either in an object-oriented or in a function-oriented style.

The CGI module itself is a complex, even extreme, application that includes numerous features from Perl. Not surprisingly, some experts claim that if you understand the GGI.pm module, you'll understand Perl! CGI.pm, written by Lincoln Stein, is the module most developers use to build Web applications with Perl. You can retrieve the module from CPAN, and see current documentation at the CGI.pm page.

Unfortunately, complex libraries often contain subtleties that make them more difficult to use. On the other hand, the complications of the CGI module appear only under certain conditions. Take, for example, the implementation of the file upload function in HTML form, where the documentation neglects to tell us about all of the features. In Listing 1 below, the user can upload a file to a Web server. When the form is processed, the script retrieves the contents of the file and displays them in the browser; they are enclosed by <pre> and </pre> tags.

Listing 1. File upload sample code

#!/usr/bin/perl -Tw

use CGI ':standard';
use strict;

my $out.= start_multipart_form.filefield(-name.=> 'upload');
$out .= br.submit('submit','Send').end_form;
my$file.= param('upload'); #the filename returned is also a file handle
if(request_method eq 'POST' &&
defined $file && ref $file && ref $file eq 'Fh')
{
local $/.= undef; #read the whole file
$out .= pre ||''; #takes care not to send uninitialized value
close $file if $CGI::OS ne 'UNIX'; #such as Win32 platforms
}

print header,start_html('Sample upload page'),$out,end_html;
The "$out .= pre <$file>||'';" line is absolutely necessary here. When a user sends an empty file or enters an invalid file name, the diamond operator returns the uninitialized defined value, causing a crash of the pre function.

Moreover, $file needs quite a few verification points, because when the POST method is used for a simple form, $file is no longer the file handle.

And some operating systems require explicit closure of the temporary file. This is true for Win32, but it is not true for UNIX platforms.

These are all subtle features of the CGI module that the documentation does not tell us about, and that make the module a little more complex under these conditions.

Tutorial Pages:
» A Close Look at the CGI.pm Module
» The CGI module
» Displaying Script Errors in the Browser
» Displaying script errors in the browser
» CGI::LogCarp Usage and Shortcomings
» Standard Modules of CGI::* Type
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Creating Perl Modules for Web Sites
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources