📄 cgi::carp.3
字号:
.Vb 5\& carpout(LOG);\& carpout(main::LOG);\& carpout(main\*(AqLOG);\& carpout(\eLOG);\& carpout(\e\*(Aqmain::LOG\*(Aq);\&\& ... and so on.Ve.PPFileHandle and other objects work as well..PPUse of \fIcarpout()\fR is not great for performance, so it is recommendedfor debugging purposes or for moderate-use applications. A futureversion of this module may delay redirecting \s-1STDERR\s0 until one of theCGI::Carp methods is called to prevent the performance hit..SH "MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW".IX Header "MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW"If you want to send fatal (die, confess) errors to the browser, ask toimport the special \*(L"fatalsToBrowser\*(R" subroutine:.PP.Vb 2\& use CGI::Carp qw(fatalsToBrowser);\& die "Bad error here";.Ve.PPFatal errors will now be echoed to the browser as well as to the log. CGI::Carparranges to send a minimal \s-1HTTP\s0 header to the browser so that even errors thatoccur in the early compile phase will be seen.Nonfatal errors will still be directed to the log file only (unless redirectedwith carpout)..PPNote that fatalsToBrowser does \fBnot\fR work with mod_perl version 2.0and higher..Sh "Changing the default message".IX Subsection "Changing the default message"By default, the software error message is followed by a note tocontact 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\&\fIset_message()\fR routine. This is not imported by default; you shouldimport it on the \fIuse()\fR line:.PP.Vb 2\& use CGI::Carp qw(fatalsToBrowser set_message);\& set_message("It\*(Aqs not a bug, it\*(Aqs a feature!");.Ve.PPYou may also pass in a code reference in order to create a customerror message. At run time, your code will be called with the textof the error message that caused the script to die. Example:.PP.Vb 9\& use CGI::Carp qw(fatalsToBrowser set_message);\& BEGIN {\& sub handle_errors {\& my $msg = shift;\& print "<h1>Oh gosh</h1>";\& print "<p>Got an error: $msg</p>";\& }\& set_message(\e&handle_errors);\& }.Ve.PPIn order to correctly intercept compile-time errors, you should call\&\fIset_message()\fR from within a BEGIN{} block..SH "DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS".IX Header "DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS"If fatalsToBrowser in conjunction with set_message does not provide you with all of the functionality you need, you can go one step further by specifying a function to be executed any time a scriptcalls \*(L"die\*(R", has a syntax error, or dies unexpectedly at runtimewith a line like \*(L"undef\->\fIexplode()\fR;\*(R"..PP.Vb 7\& use CGI::Carp qw(set_die_handler);\& BEGIN {\& sub handle_errors {\& my $msg = shift;\& print "content\-type: text/html\en\en";\& print "<h1>Oh gosh</h1>";\& print "<p>Got an error: $msg</p>";\&\& #proceed to send an email to a system administrator,\& #write a detailed message to the browser and/or a log,\& #etc....\& }\& set_die_handler(\e&handle_errors);\& }.Ve.PPNotice that if you use \fIset_die_handler()\fR, you must handle sending\&\s-1HTML\s0 headers to the browser yourself if you are printing a message..PPIf you use \fIset_die_handler()\fR, you will most likely interfere with the behavior of fatalsToBrowser, so you must use this or that, not both..PPUsing \fIset_die_handler()\fR sets SIG{_\|_DIE_\|_} (as does fatalsToBrowser),and there is only one SIG{_\|_DIE_\|_}. This means that if you are attempting to set SIG{_\|_DIE_\|_} yourself, you may interfere with this module's functionality, or this module may interfere with your module's functionality..SH "MAKING WARNINGS APPEAR AS HTML COMMENTS".IX Header "MAKING WARNINGS APPEAR AS HTML COMMENTS"It is now also possible to make non-fatal errors appear as \s-1HTML\s0comments embedded in the output of your program. To enable thisfeature, export the new \*(L"warningsToBrowser\*(R" subroutine. Since sendingwarnings to the browser before the \s-1HTTP\s0 headers have been sent wouldcause an error, any warnings are stored in an internal buffer untilyou call the \fIwarningsToBrowser()\fR subroutine with a true argument:.PP.Vb 4\& use CGI::Carp qw(fatalsToBrowser warningsToBrowser);\& use CGI qw(:standard);\& print header();\& warningsToBrowser(1);.Ve.PPYou may also give a false argument to \fIwarningsToBrowser()\fR to preventwarnings from being sent to the browser while you are printing somecontent where \s-1HTML\s0 comments are not allowed:.PP.Vb 5\& warningsToBrowser(0); # disable warnings\& print "<script type=\e"text/javascript\e"><!\-\-\en";\& print_some_javascript_code();\& print "//\-\-></script>\en";\& warningsToBrowser(1); # re\-enable warnings.Ve.PPNote: In this respect \fIwarningsToBrowser()\fR differs fundamentally from\&\fIfatalsToBrowser()\fR, which you should never call yourself!.SH "OVERRIDING THE NAME OF THE PROGRAM".IX Header "OVERRIDING THE NAME OF THE PROGRAM"CGI::Carp includes the name of the program that generated the error orwarning in the messages written to the log and the browser window.Sometimes, Perl can get confused about what the actual name of theexecuted program was. In these cases, you can override the programname that CGI::Carp will use for all messages..PPThe quick way to do that is to tell CGI::Carp the name of the programin its use statement. You can do that by adding\&\*(L"name=cgi_carp_log_name\*(R" to your \*(L"use\*(R" statement. For example:.PP.Vb 1\& use CGI::Carp qw(name=cgi_carp_log_name);.Ve.PP\&. If you want to change the program name partway through the program,you can use the \f(CW\*(C`set_progname()\*(C'\fR function instead. It is notexported by default, you must import it explicitly by saying.PP.Vb 1\& use CGI::Carp qw(set_progname);.Ve.PPOnce you've done that, you can change the logged name of the programat any time by calling.PP.Vb 1\& set_progname(new_program_name);.Ve.PPYou can set the program back to the default by calling.PP.Vb 1\& set_progname(undef);.Ve.PPNote that this override doesn't happen until after the program hascompiled, so any compile-time errors will still show up with thenon-overridden program name.SH "CHANGE LOG".IX Header "CHANGE LOG"1.29 Patch from Peter Whaite to fix the unfixable problem of CGI::Carp not behaving correctly in an \fIeval()\fR context..PP1.05 \fIcarpout()\fR added and minor corrections by Marc Hedlund <hedlund@best.com> on 11/26/95..PP1.06 \fIfatalsToBrowser()\fR no longer aborts for fatal errors within \fIeval()\fR statements..PP1.08 \fIset_message()\fR added and \fIcarpout()\fR expanded to allow for FileHandle objects..PP1.09 \fIset_message()\fR now allows users to pass a code \s-1REFERENCE\s0 for really custom error messages. croak and carp are now exported by default. Thanks to Gunther Birznieks for the patches..PP1.10 Patch from Chris Dean (ctdean@cogit.com) to allow module to run correctly under mod_perl..PP1.11 Changed order of > and < escapes..PP1.12 Changed \fIdie()\fR on line 217 to CORE::die to avoid \fB\-w\fR warning..PP1.13 Added \fIcluck()\fR to make the module orthogonal with Carp. More mod_perl related fixes..PP1.20 Patch from Ilmari Karonen (perl@itz.pp.sci.fi): Added \fIwarningsToBrowser()\fR. Replaced <\s-1CODE\s0> tags with <\s-1PRE\s0> in \fIfatalsToBrowser()\fR output..PP1.23 \fIineval()\fR now checks both $^S and inspects the message for the \*(L"eval\*(R" pattern (hack alert!) in order to accommodate various combinations of Perl and mod_perl..PP1.24 Patch from Scott Gifford (sgifford@suspectclass.com): Add support for overriding program name..PP1.26 Replaced CORE::GLOBAL::die with the evil \f(CW$SIG\fR{_\|_DIE_\|_} because the former isn't working in some people's hands. There is no such thing as reliable exception handling in Perl..PP1.27 Replaced tell \s-1STDOUT\s0 with bytes=tell \s-1STDOUT\s0..SH "AUTHORS".IX Header "AUTHORS"Copyright 1995\-2002, Lincoln D. Stein. All rights reserved..PPThis library is free software; you can redistribute it and/or modifyit under the same terms as Perl itself..PPAddress bug reports and comments to: lstein@cshl.org.SH "SEE ALSO".IX Header "SEE ALSO"Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,CGI::Response if (defined($CGI::Carp::PROGNAME)) { \f(CW$file\fR = \f(CW$CGI::Carp::PROGNAME\fR; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -