When developing web applications and something goes wrong, its slow and tedious to have to go look up the problem in a error log on the server. Fortunately using a cgi module called Carp can help.
The normal way of reporting errors within a Perl script is to use the warn() or die() functions to report and generate errors. You might also use the Carp module, which provides additional levels of control over the messages that you raise, particularly within modules.
An additional module, CGI::Carp provides much of the same functionality as the Carp module. It is specially designed to be used within web scripts where you want error information to go to a specific log, rather than the default web server log (for example, one generated by Apache), or where you want the information to go to the web page in a controlled fashion.
The standard Carp module provides alternatives for the warn() and die() functions that provide more information and are more friendly in terms of providing the location of the error. When used within a module, for example, the error message includes the module name and line number.
Within the Carp module, the four main functions are carp(), which is a synonym for a warning message, and croak(), which is like die() and also terminates the script. cluck() and confess() are like warn() and die() respectively but provide a stack back trace from the point where the error was raised.
If you use both the Carp and CGI::Carp modules, then the standard functions, such as warn(), die(), and Carp module functions, croak(), confess(), and carp() will now write their error information out to the configured HTTP server log with a date/time stamp and script source.
An alternative to using the HTTP server error log is to use CGI::Carp and make use of the carpout() function. This accepts a single argument, the filehandle of the file where you want errors (normally sent to STDERR) to be written. You have to import explicitly the carpout() function. You can see a simple example.
#!/usr/bin/perl use strict; use warnings;
use CGI::Carp qw/carpout/; use IO::File;
my $logfile = IO::File->new('browser.log','w') or die "Couldn't open logfile: $!\n";
warn "Some error must have occurred\n";
he information generated in the log is identified with both the date and the name of the script that generated the output:
[Thu Aug 20 01:35:56 2015] carpout.cgi: Some error must have occurred
All of these standardized methods assume that you want your error information to go to a log file. But, you may not always have access to the logs or want to be logged in to your browser to get the information.
The CGI::Carp function therefore also provides a fatalsToBrowser option that redirects fatal error messages ( die(),confess() ) back to the browser, as well as to the web server log. This ensures that your users see the errors generated by the script. Non-fatal errors (warn() and carp()) will continue to go to the error log as normal.
To use, you must specify it as an option when loading the CGI::Carp module, use CGI::Carp qw/fatalsToBrowser/;. We can add this to our file browsing script to ensure that errors are correctly reported and identified.