Clear questions and runnable code
get the best and fastest answer
CGI::Carpby redcloud (Parson)
|on Sep 14, 2000 at 00:56 UTC||Need Help??|
Item Description: CGI routines for reporting errors
Review Synopsis: An excellent debugging tool for CGI scripts
If you're trying to debug a CGI script, CGI::Carp could be your best friend. It makes getting error messages from CGI scripts with a browser practically as easy as from other scripts at the command line.
My favorite feature is also the easiest to use. Import the special symbol
fatalsToBrowser like this:
use CGI::Carp qw(fatalsToBrowser);
Now, if have a syntax error in your script that would abort execution due to compilation errors, say, instead of getting a server error from your HTTP server that isn't very informative, you get a message telling you that there are compilation errors. If I used perl -c more often, I probably wouldn't be so attached to this feature.
On the downside, information about what line the error is at doesn't show up in your browser. It would be nice if it did. However, you can retrieve that information from your HTTP server's error log.
Once you get past syntax errors, anything that you want to die() or croak() about will go to the browser, too, if you've imported fatalsToBrowser. If you want warnings to go to the browser as well, import the carpout() function and use it to redirect STDERR.
You can also use carpout() to redirect STDERR to your own log file, instead of the server's error log. Here's the example from perldoc CGI::Carp:
These conveniences make CGI programming a lot easier, and they make CGI::Carp one of my favorite modules.