📄 ch19_03.htm
字号:
<HTML><HEAD><TITLE>Recipe 19.2. Redirecting Error Messages (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:20Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script"><LINKREL="next"HREF="ch19_04.htm"TITLE="19.3. Fixing a 500 Server Error"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.1. Writing a CGI Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_04.htm"TITLE="19.3. Fixing a 500 Server Error"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.3. Fixing a 500 Server Error"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-15202">19.2. Redirecting Error Messages</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1000005384">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005388-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005388-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005388-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005388-3"></A></A></H3><PCLASS="para">You're having trouble tracking down your script's warnings and error messages, or your script's STDERR output is confusing your server.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-272">Solution</A></H3><PCLASS="para">Use the CGI::Carp module from the standard Perl distribution to cause all messages going out STDERR to be prefixed with the name of the application and the current date. You can also send warnings and errors to a file or the browser if you wish.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-278">Discussion</A></H3><PCLASS="para">Tracking down error messages from CGI scripts is notoriously annoying. Even if you manage to find the server error log, you still can't determine which message came from which script, or at what time. Some unfriendly web servers even abort the script if it has the audacity to emit anything out its STDERR before the <CODECLASS="literal">Content-Type</CODE> header is generated, which means the <BCLASS="emphasis.bold">-w</B> flag can get you into trouble.</P><PCLASS="para">Enter the <ACLASS="indexterm"NAME="ch19-idx-1000005399-0"></A>CGI::Carp module. It replaces <CODECLASS="literal">warn</CODE> and <CODECLASS="literal">die</CODE>, plus the normal Carp module's <CODECLASS="literal">carp</CODE>, <CODECLASS="literal">croak</CODE>, and <CODECLASS="literal">confess</CODE> functions with more verbose and safer versions. It still sends them to the normal server error log.</P><PRECLASS="programlisting">use CGI::Carp;warn "This is a complaint";die "But this one is serious";</PRE><PCLASS="para">The following use of CGI::Carp also redirects errors to a file of your choice, placed in a BEGIN block to catch compile-time warnings as well:</P><PRECLASS="programlisting">BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/var/local/cgi-logs/mycgi-log") or die "Unable to append to mycgi-log: $!\n"; carpout(*LOG);}</PRE><PCLASS="para">You can even arrange for fatal errors to return to the client browser, which is nice for your own debugging but might confuse the end user.</P><PRECLASS="programlisting">use CGI::Carp qw(fatalsToBrowser);die "Bad error here";</PRE><PCLASS="para">Even if the error happens before you get the HTTP header out, the module will try to take care of this to avoid the dreaded <CODECLASS="literal">500</CODE> <CODECLASS="literal">Server</CODE> <CODECLASS="literal">Error</CODE>. Normal warnings still go out to the server error log (or wherever you've sent them with <CODECLASS="literal">carpout</CODE>) with the application name and date stamp prepended.<ACLASS="indexterm"NAME="ch19-idx-1000005390-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005390-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005390-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-314">See Also</A></H3><PCLASS="para">The documentation for the standard CGI::Carp module, the discussion on BEGIN in <ACLASS="xref"HREF="ch12_04.htm"TITLE="Delaying use Until Run Time">Recipe 12.3</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.1. Writing a CGI Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_04.htm"TITLE="19.3. Fixing a 500 Server Error"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.3. Fixing a 500 Server Error"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.1. Writing a CGI Script</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.3. Fixing a 500 Server Error</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -