Anyone who has written a CGI script knows how annoying bugs are. Often, you'll get a SERVER ERROR message with little clue as to what happened. This week, learn how to use a simple debugger to examine your CGI scripts. Click on these links to watch the debugger in action:
Now, download the source to the debugger: Then, copy it to your cgi-bin directory. After making the script executable (chmod ugo+x debug.pl), you can invoke it with a URL similar to the following:http://yourserver.com/cgi-bin/debug.pl/script-to-debug
For example, if you had a script named /cgi-bin/dothis.pl, then you could use the debugger by doing the following:
http://yourserver.com/cgi-bin/debug.pl/dothis.pl
You can also do form POST and GET operations to the debugger. For example, if you had HTML like the following:
<form method=POST action="/cgi-bin/script.pl">
It could be changed to
<form method=POST action="/cgi-bin/debug.pl/script.pl">
to run your "script.pl" in the debugger.
The output from your cgi script will be captured by the debugger and displayed. First, the debugger shows the current environment variables. After that, it runs the cgi script and captures its output. You'll notice that the current directory and full name of the script along with its process id (PID) are displayed.
The debugger can find the normal HTTP headers and displays them in green. When the actual content starts, html tags will be displayed in blue. All other data will be left unformatted. If there appears to be an error, you'll see a red message describing it. (There may be a few anomalies in the parsing and coloring, so don't put too much faith in the colors that come out of the debugger.)
Depending upon how your web site is set up, you may encounter an error message like "Can't stat current directory." This message comes from the Cwd package (use Cwd;). If you see that message, delete
use Cwd;
and change or eliminate the following line:
# current directory
$dir=htmlize(cwd);
Note: This program has been tested on many flavors of Unix running the Apache web server. It may not, however, work correctly on all web servers, especially non-Unix ones.