| the complete webmaster | ||||
| tutorials | reviews | reference | ||
|
Debugging CGI ScriptsAnyone 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.) Debugging the DebuggerTo make the debugger simpler to program, the standard error (STDERR) stream from your cgi script is redirected to standard output (STDOUT). Normally, STDERR would end up in your server's error logs. Here, it will be intermixed with the normal STDOUT output from your script. I agree that this is somewhat confusing. Ideally, STDERR should be displayed in a separate section of the debugger. (There are, however, certain problems that arise. See the man page for IPC::Open3 for more information.)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 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.
Author:
Doug Steinwand
Date: [12/23/97]
More articles about CGI
More articles by Doug Steinwand Author Biography |
| write for us | about us | advertise |
Copyright 1997, 1998 A Big Lime. All rights reserved.