I have a Perl script that isn\'t working and I don\'t know how to start narrowing down the problem. What can I do?
Note: I\'m adding the question because I rea
die
statements and other fatal run-time and compile-time errors get
printed to STDERR
, which can be hard to find and may be conflated with
messages from other web pages at your site. While you're debugging your
script, it's a good idea to get the fatal error messages to display in your
browser somehow.
One way to do this is to call
use CGI::Carp qw(fatalsToBrowser);
at the top of your script. That call will install a $SIG{__DIE__}
handler (see perlvar)display fatal errors in your browser, prepending it with a valid header if necessary. Another CGI debugging trick that I used before I ever heard of CGI::Carp
was to
use eval
with the DATA
and __END__
facilities on the script to catch compile-time errors:
#!/usr/bin/perl
eval join'', ;
if ($@) { print "Content-type: text/plain:\n\nError in the script:\n$@\n; }
__DATA__
# ... actual CGI script starts here
This more verbose technique has a slight advantage over CGI::Carp
in that it will catch more compile-time errors.
Update: I've never used it, but it looks like CGI::Debug, as Mikael S
suggested, is also a very useful and configurable tool for this purpose.