When Perl encounters an error, it sends a message to STDERR (standard error). When we run our program from the command line, STDERR is by default the screen. However, when we run the program through the Web, the Web server usually redirects STDERR to its error log file. It is possible to look at that file, but there are better solutions to track our errors in CGI-programs than that.
program_name.pl name1=value1 name2=value2If your value contains spaces, or other special characters that might be wrongly understood by the unix shell, use quotes or backslashes. Examples:
program_name.pl "name1='word1 word2'" "name2='word3 word4 word5'"
program_name.pl "name1=word1\ word2" "name2=word3\ word4\ word5"
sub cgi_message {
my($string) = @_;
print "\n<P><B><FONT SIZE =+1 COLOR=RED>Error:</FONT></B> $string<P>\n";
return 1;
}
You can call this subroutine by sending some message as a parameter.
For example, when you open a file, instead of:
... or die "some informative message";
write:
... or cgi_message ("some informative message");
perldoc CGIand look at the section called "RETRIEVING CGI ERRORS".
Read about this module by typing the following on the unix command line:
perldoc CGI::Carp
A simple suggestion is to place the following command somewhere
on the top of your program:
use CGI::Carp qw(fatalsToBrowser);
This will send all fatal errors to the browser window, including ones that occur during
the compilation phase. In this case you can use 'die' in your program.
For example, if the CGI-program contains the command
open (IN, "filename") or die "cannot open file \"filename\": $!";
and the file "filename" cannot be opened, a message like this will appear on the browser:
cannot open file "filename": No such file or directory at /users/lhchalif/public_html/test1.cgi line 31.
For help, please send mail to the webmaster (webmaster@bioinfo.weizmann.ac.il), giving this error message and the time and date of the error.
This is a good solution for the debugging phase, but may be problematic when the program is "live" on the web for use by wide audience. The reasons are:
It is possible, though, to change the message sent by 'fatalsToBrowser'. Read about it in the section 'Changing the default message' of the CGI::Carp documentation.