Perl-Sensitive Sunglasses | |
PerlMonks |
perlman:CGI::Carpby root (Monk) |
on Dec 23, 1999 at 00:50 UTC ( [id://1168]=perlfunc: print w/replies, xml ) | Need Help?? |
CGI::CarpSee the current Perl documentation for CGI::Carp. Here is our local, out-dated (pre-5.6) version: CGI::Carp - CGI routines for writing to the HTTPD (or other) error log
use CGI::Carp;
croak "We're outta here!"; confess "It was my fault: $!"; carp "It was your fault!"; warn "I'm confused"; die "I'm dying.\n";
CGI scripts have a nasty habit of leaving warning messages in the error logs that are neither time stamped nor fully identified. Tracking down the script that caused the error is a pain. This fixes that. Replace the usual
use Carp; with
use CGI::Carp
And the standard
For example:
[Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3. [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied. [Fri Nov 17 21:40:43 1995] test.pl: I'm dying.
REDIRECTING ERROR MESSAGESBy default, error messages are sent to STDERR. Most HTTPD servers direct STDERR to the server's error log. Some applications may wish to keep private error logs, distinct from the server's error log, or they may wish to direct error messages to STDOUT so that the browser will receive them.
The
use CGI::Carp qw(carpout);
The
BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or die("Unable to open mycgi-log: $!\n"); carpout(LOG); }
The real STDERR is not closed -- it is moved to SAVEERR. Some servers, when dealing with CGI scripts, close their connection to the browser when the script closes STDOUT and STDERR. SAVEERR is used to prevent this from happening prematurely.
You can pass filehandles to
carpout(\*LOG); This looks weird to mere mortals however, so the following syntaxes are accepted as well:
carpout(LOG); carpout(main::LOG); carpout(main'LOG); carpout(\LOG); carpout(\'main::LOG');
... and so on FileHandle and other objects work as well.
Use of
MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOWIf you want to send fatal (die, confess) errors to the browser, ask to import the special ``fatalsToBrowser'' subroutine:
use CGI::Carp qw(fatalsToBrowser); die "Bad error here"; Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp arranges to send a minimal HTTP header to the browser so that even errors that occur in the early compile phase will be seen. Nonfatal errors will still be directed to the log file only (unless redirected with carpout).
Changing the default message
By default, the software error message is followed by a note to contact the Webmaster by e-mail with the time and date of the error. If this message is not to your liking, you can change it using the
use CGI::Carp qw(fatalsToBrowser set_message); set_message("It's not a bug, it's a feature!"); You may also pass in a code reference in order to create a custom error message. At run time, your code will be called with the text of the error message that caused the script to die. Example:
use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { sub handle_errors { my $msg = shift; print "<h1>Oh gosh</h1>"; print "Got an error: $msg"; } set_message(\&handle_errors); }
In order to correctly intercept compile-time errors, you should call
CHANGE LOG
1.05
1.06
1.08
1.09
1.10 Patch from Chris Dean (ctdean@cogit.com) to allow module to run correctly under mod_perl.
AUTHORSLincoln D. Stein <lstein@genome.wi.mit.edu> Feel free to redistribute this under the Perl Artistic License.
SEE ALSOCarp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form, CGI::Response |
|