#12 Printing Debugging Information

CGI programming requires different skills. Not only do you have to know Perl programming, but also HTML and HTML forms. Sometimes what's in the form and what you think is in the form differ. As a result, the inputs to your CGI program aren't what it expects and the program fails.

To help locate errors, it's nice to know the exact inputs to a program. This shows the use of a debug function that prints out all the CGI and environment parameters, giving the programmer a lot of extremely useful debugging information.

The Code

  1 #!/usr/bin/perl -T
  2 use strict;
  3
  4 use CGI::Thin;
  5 use CGI::Carp  qw(fatalsToBrowser);
  6 use HTML::Entities;
  7
  8 #
  9 # debug -- print debugging information to the screen
 10 #
 11 sub debug()
 12 {
 13     print "<H1>DEBUG INFORMATION</H1>
";
 14     print "<H2>Form Information</H2>
";
 15     my %form_info = Parse_CGI();
 16     foreach my $cur_key (sort keys %form_info) {
 17         print "<BR>";
 18         if (ref $form_info{$cur_key}) {
 19             foreach my $value (@{$form_info{$cur_key}}) {
 20                 print encode_entities($cur_key), " = ",
 21                       encode_entities($value), "
";
 22             }
 23         } else {
 24             print encode_entities($cur_key), " = ",
 25                   encode_entities(
 26                       $form_info{$cur_key}), "
";
 27         }
 28     }
 29     print "<H2>Environment</H2>
";
 30     foreach my $cur_key (sort keys %ENV) {
 31         print "<BR>";
 32         print encode_entities($cur_key), " = ",
 33         encode_entities($ENV{$cur_key}), "
";
 34     }
 35 }
 36
 37 # Call the program to print out the stuff
 38 print "Content-type: text/html
";
 39 print "
";
 40 debug();

Using the Function

To use the function, simply put it in your CGI program and call it.

The Results

Here's the result of running the script. The form we filled in to get to this script took two parameters, a width and a height. From the debug output you can see the values we filled in.

You can also see all the environment information passed to us by the CGI system.


How It Works

The script uses the Parse_CGI function to grab all the CGI parameters. These are stored in the hash %form_hash:

 15     my %form_info = Parse_CGI();

The hash creates a

form_variable => value

mapping. But there is a problem. Some form elements, like a multiple-selection list, can have more than one value. In that case the "value" returned is not a real value but instead a reference to an array of values.

In order to print things, your code needs to know the difference between the two. This is done using the ref function. If you have an array reference, you print the elements. If you have something else, you just print the value:

 16     foreach my $cur_key (sort keys %form_info) {
 17         print "<BR>";
 18         if (ref $form_info{$cur_key}) {
 19             foreach my $value (@{$form_info{$cur_key}}) {
 20                 print encode_entities($cur_key), " = ",
 21                       encode_entities($value), "
";
 22             }
 23         } else {
 24             print encode_entities($cur_key), " = ",
 25                   encode_entities(
 26                       $form_info{$cur_key}), "
";
 27         }
 28     }

The environment is printed using a similar system. Since you don't have to worry about multiple values this time, the printing is a bit simpler:

 30     foreach my $cur_key (sort keys %ENV) {
 31         print "<BR>";
 32         print encode_entities($cur_key), " = ",
 33         encode_entities($ENV{$cur_key}), "
";
 34     }

Between the environment and the CGI parameters, you've printed every input to a CGI program.

Hacking the Script

In the field, it would be nice to be able to turn on and off the debugging output at will. One technique is use a remote shell on the server to create a file such as /tmp/cgi_debug and, if it is present, turn on the debugging.

The debug function can also be augmented to print out more information, such as the state of program variables or the contents of information files.

Printing information to the screen is one of the more useful ways of getting debugging information out of a CGI system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset