CGI Debugging
When programming CGI scripts for a web-based application, one common problem is that if there is an error all you see is "Internal Server Error" and you have to go look in the server’s log file for details. But sometimes you don’t have access to the log file, such as when it is hosted on an ISP’s server. Here’s a way to get those errors sent to the screen where they can be of use to you.
Just put this at the top of the script:
BEGIN {
# Turn off buffering of STDOUT
$| = 1;
# Redirect STDERR to STDOUT
open STDERR, ">&STDOUT";
# HTTP header
print "Content-Type: text/htmlnn";
}
Now, all errors that occur after that point will be sent to the browser as part of STDOUT rather than put in the logfile as STDERR, since most web servers (Apache in particular) directs STDOUT to the browser and STDERR to the error log file.
Because it’s in a BEGIN block, it will be processed during the compilation phase, before your program starts running. Since a "use" statement is also execteud at the compilation phase, you must put this BEGIN block before any "use" statements to make sure that it works.
Now if one of your "use" statements causes an error, or if your program has any kind of compilation problem, you should see the result in your browser. (Note that the output will be all run together, since it probably won’t have any HTML tags like <br> to separate the lines. To fix that, right click on the page and "view source" - there, you will find the line breaks and other formatting intact.)
