CGI and Perl

Example 4: Using CGI::Carp to Debug Scripts

CGI::Carp contains a very useful feature called fatalsToBrowser. Instead of getting the typical "Server Error" message when something is wrong with your script (as you are writing it), the STDERR is formatted and outputted as HTML to the browser. This prevents you from having to run your script from the shell to find out where bugs are.

Listing 5.4 is an example of script using CGI::Carp qw(fatalsToBrowser) that will fail due to the garbage-line foo bar baz;. Instead of getting a "Server Error," the output looks like Figure 5.6.

Using the HTTP::Request Module Directly

Obviously, is very powerful. Sometimes simple CGI scripts may not need all of's bells and whistles.

The CGI::Request module is great for CGI scripts that just need access ENV variables. The CGI::Request module loads a lot faster and is less resource intensive than for simple tasks like this.

The GetRequest method in CGI::Request parses the ENV variable and breaks it down into name-value pairs. GetRequest also will remove any dangerous, meta-, or other illegal characters that could cause a security risk on your system.

Insert the following lines at the beginning of your script:

use CGI::Request

The most frequently used headers can be accessed through the following methods. These methods can be used both to read and to set the value of a header. The header value is set if you pass an argument to the method. The old header value is always returned.

$h->date This header represents the date and time at which the message was originated. Example: $h->date(time); # set current date
$h->expires This header gives the date and time after which the entity should be considered stale.
$h->if_modified_since This header is used to make a request conditional. If the requested resource has not been modified since the time specified in this field, then the server will return a 304 Not Modified response instead of the document itself.
$h->last_modified This header indicates the date and time at which the resource was last modified. Example: # check if document is more than 1 # hour old if ($h->last_modified<time - 60*60) {
$h->content_type The content-type header field indicates the media type of the message content. Example: $h->content_type(`text/html');
$h->content_encoding The content-encoding header field is used as a modifier to the media type. When present, its value indicates what additional encoding mechanism has been applied to the resource.
$h->content_length A decimal number indicating the size in bytes of the message content.
$h->user_agent This header field is used in request messages and contains information about the user agent originating the request. Example: $h->user_agent(`Mozilla/1.2');
$h->server The server header field contains information about the software being used by the origin server program handling the request.
$h->from This header should contain an Internet e-mail address for the human user who controls the requesting user agent. The address should be machine-usable, as defined by RFC822. Example: $h->from(`Gisle Aas <[email protected]>`);
$h->referer Used to specify the address (URI) of the document from which the requested resource address was obtained.
$h->uri This header field may contain one or more URIs by which the resource origin of the entity can be identified.
$h->www_authenticate This header must be included as part of a 401 Unauthorized response. The field value consists of a challenge that indicates the authentication scheme and parameters applicable to the requested URI.
$h->authorization A user agent that wishes to authenticate itself with a server may do so by including this header.
$h->authorization_basic This method lets you get/set an authorization header that uses the "Basic Authentication Scheme." It will return a list of two values. The first is the username and the second the password. It also expects two arguments when it is used to set the header value. Example: $h->authorization_basic(`user', `passwd');