CGI and Perl

Perl5 and the WWW Libraries in Action

Perl, with the help of or the WWW libraries, allows complete control over every aspect of the client/server HTTP transaction. Using the CGI::Request WWW library in your scripts to manage and process variables passed between the client and the server during a transaction makes your code more secure, easier to maintain, and easier to write. versus Individual libwww Modules and the modular WWW libraries are very similar in function but very different in purpose. includes all of the functionality of the individual modules, and in most cases, more. In fact, Lincoln Stein, the author of and the WWW modules, developed the module in parallel with the CGI, URL, and HTML modules. The individual modules are designed to provide a very optimized solution to about nine different aspects of programming with Perl for the Web. Modules exist for the Web functions listed in Table 5.4. Table 5.4. Function of libwww modules.

Module Function
CGI:: Allows control over HTTP request and response headers. Mini HTTP server, CGI-based imagemapping, "smart" forms.
File:: Parsing of directory listings and a persistent counter.
Font:: Interface to Adobe Font Metrics Files.
HTML:: Creation and parsing of HTML elements. Format HTML as text or PostScript.
HTTP:: Generation of HTTP requests, responses, and headers. Content negotiation and status code processing.
LWP:: Low-level I/O, socket interface, file type recognition.
MIME:: Encoding and decoding Base64 and QP.
URI:: Filtering, processing of URIs, elimination of unsafe characters in strings.
WWW:: Parse robot.txt files.
Think of each of the individual modules as a very focused and optimized solution to a specific task. Most of the time, you will be writing scripts that use features from more than one of the preceding modules. If you have to load any more than three modules, it is actually more efficient to just load the entire Loading each of the modules separately actually is more resource intensive due to the overhead associated with loading multiple files. The module also has a more robust feature set than the individual modules.

Anything your CGI prints to STDOUT gets sent back to the Web server and out to the browser, unless you are using nph-CGIs, in which case, STDOUT goes directly to the browser without being parsed by the server. Traditionally, before Perl5 and, HTML forms were created by CGI scripts by printing the HTML form elements to STDOUT. Traditionally, the name/value pairs would have to be URI-decoded from either the URI (GET method) or STDIN (POST method). Back in the days of Perl 4, include scripts such as were written to deal with these nuisances. However, there were still problems. Using, the state of a form (the
contents of the variables) was lost from invocation to invocation because neither the browser nor the server retained data from a previous transaction, debugging was difficult, namespace was trampled, and the code was messy and would emit errors before the HTTP response headers were printed if Perl was run in strict mode.


If you still aren't convinced, a complete rundown on all of the benefits of using and Perl5 over the old methods such as can be found at the following URL:!cgi-lib.html was created to address these problems and build a wide and extensible bridge between your CGI Perl script and the CGI interface. With, HTML forms can be generated easily by making a few Perl function calls. When you use to generate HTML forms, the values of the input fields from previous queries (if there were any) are automatically used to initialize the form, so the state of the form is preserved from invocation to invocation. URI encoding and
decoding of special characters is handled transparently by GET and POST methods are handled appropriately. If your script is run from the UNIX shell, the option of inputting variables from the command line is given. In short, takes care of almost all of the gory details of CGI and lets you focus on writing your program.

The easiest way to learn to use or any of the modules is to simply start using them in your scripts.

by BrainBellupdated