📄 ch12_13.htm
字号:
<HTML><HEAD><TITLE>Recipe 12.12. Reporting Errors and Warnings Like Built-Ins (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:41:55Z"><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="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"><LINKREL="prev"HREF="ch12_12.htm"TITLE="12.11. Overriding Built-In Functions"><LINKREL="next"HREF="ch12_14.htm"TITLE="12.13. Referring to Packages Indirectly"></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="ch12_12.htm"TITLE="12.11. Overriding Built-In Functions"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.11. Overriding Built-In Functions"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_14.htm"TITLE="12.13. Referring to Packages Indirectly"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.13. Referring to Packages Indirectly"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch12-30500">12.12. Reporting Errors and Warnings Like Built-Ins</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1176">Problem<ACLASS="indexterm"NAME="ch12-idx-1000005264-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005264-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005264-2"></A><ACLASS="indexterm"NAME="ch12-idx-1000005264-3"></A><ACLASS="indexterm"NAME="ch12-idx-1000005264-4"></A></A></H3><PCLASS="para">You want to generate errors and warnings in your modules, but when you use <CODECLASS="literal">warn</CODE> or <CODECLASS="literal">die</CODE>, the user sees your own filename and line number. You'd like your functions to act like built-ins and report messages from the perspective of the user's code not your own.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1182">Solution</A></H3><PCLASS="para">The standard <ACLASS="indexterm"NAME="ch12-idx-1000005265-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005265-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005265-2"></A>Carp module provides functions to do this. Use <CODECLASS="literal">carp</CODE> instead of <CODECLASS="literal">warn</CODE>. Use <CODECLASS="literal">croak</CODE> (for a short message) and <CODECLASS="literal">confess</CODE> (for a long message) instead of <CODECLASS="literal">die</CODE>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1188">Discussion</A></H3><PCLASS="para">Like built-ins, some of your module's functions generate warnings or errors if all doesn't go well. Think about <CODECLASS="literal">sqrt</CODE>: when you pass it a negative number (and you haven't used the Math::Complex module), an exception is raised, producing a message such as <CODECLASS="literal">"Can't</CODE> <CODECLASS="literal">take</CODE> <CODECLASS="literal">sqrt</CODE> <CODECLASS="literal">of</CODE> <CODECLASS="literal">-3</CODE> <CODECLASS="literal">at</CODE> <CODECLASS="literal">/tmp/negroot</CODE> <CODECLASS="literal">line</CODE> <CODECLASS="literal">17"</CODE>, where <EMCLASS="emphasis">/tmp/negroot</EM> is the name of your own program. But if you write your own function that <CODECLASS="literal">die</CODE>s, perhaps like this:</P><PRECLASS="programlisting">sub even_only { my $n = shift; die "$n is not even" if $n & 1; # one way to test #....}</PRE><PCLASS="para">then the message will say it's coming from the file your <CODECLASS="literal">even_only</CODE> function was itself compiled in, rather than from the file the user was in when they called your function. That's where the Carp module comes in handy. Instead of using <CODECLASS="literal">die</CODE>, use <CODECLASS="literal">croak</CODE> instead:</P><PRECLASS="programlisting">use Carp;sub even_only { my $n = shift; croak "$n is not even" if $n % 2; # here's another #....}</PRE><PCLASS="para">If you just want to complain about something, but have the message report where in the user's code the problem occurred, call <CODECLASS="literal">carp</CODE> instead of <CODECLASS="literal">warn</CODE>. (<CODECLASS="literal">carp</CODE> and <CODECLASS="literal">croak</CODE> do not share <CODECLASS="literal">warn</CODE>'s and <CODECLASS="literal">die</CODE>'s sensitivity to a trailing newline on the message.) For example:</P><PRECLASS="programlisting">use Carp;sub even_only { my $n = shift; if ($n & 1) { # test whether odd number carp "$n is not even, continuing"; ++$n; } #....}</PRE><PCLASS="para">Many built-ins emit warnings only when the <BCLASS="emphasis.bold">-w</B> command-line switch has been used. The <CODECLASS="literal">$^W</CODE> variable (which is not meant to be a control character but rather a <CODECLASS="literal">^</CODE> followed by a <CODECLASS="literal">W</CODE>) reflects whether that switch was used. You could choose to grouse only if the user asked for complaints:</P><PRECLASS="programlisting">carp "$n is not even, continuing" if $^W;</PRE><PCLASS="para">Finally, the Carp module provides a third function: <CODECLASS="literal">confess</CODE>. This works just like <CODECLASS="literal">croak</CODE>, except that it provides a full stack backtrace as it dies, reporting who called whom and with what arguments.<ACLASS="indexterm"NAME="ch12-idx-1000005267-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005267-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005267-2"></A><ACLASS="indexterm"NAME="ch12-idx-1000005267-3"></A><ACLASS="indexterm"NAME="ch12-idx-1000005267-4"></A><ACLASS="indexterm"NAME="ch12-idx-1000005267-5"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1244">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_192.htm"><CODECLASS="literal">warn</CODE></A> and <ACLASS="olink"HREF="../prog/ch03_027.htm"><CODECLASS="literal">die</CODE></A> functions in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlmod </I>(1); the documentation for the standard Carp module, also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ACLASS="xref"HREF="ch19_03.htm"TITLE="Redirecting Error Messages">Recipe 19.2</A>; the discussion on <CODECLASS="literal">__WARN__</CODE> and <CODECLASS="literal">__DIE__</CODE> in the section on <ACLASS="olink"HREF="../prog/ch02_09.htm#PERL2-CH-2-SECT-9.4">"Global Special Arrays"</A> in <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>, in <ICLASS="filename">perlvar </I>(1), and in <ACLASS="xref"HREF="ch16_16.htm"TITLE="Installing a Signal Handler">Recipe 16.15</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="ch12_12.htm"TITLE="12.11. Overriding Built-In Functions"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.11. Overriding Built-In Functions"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="ch12_14.htm"TITLE="12.13. Referring to Packages Indirectly"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.13. Referring to Packages Indirectly"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.11. Overriding Built-In Functions</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">12.13. Referring to Packages Indirectly</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 + -