📄 ch31_22.htm
字号:
<html><head><title>use warnings (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="use warnings"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_21.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">31.22. use warnings</h2><blockquote><pre class="programlisting">use warnings; # same as importing "all"no warnings; # same as unimporting "all"use warnings::register;if (warnings::enabled()) { warnings::warn("some warning");}if (warnings::enabled("void")) { warnings::warn("void", "some warning");}</pre></blockquote><p>This lexically scoped pragma permits flexible control over Perl'sbuilt-in warnings, both those emitted by the compiler as well asthose from the run-time system.</p><p>Once upon a time, the only control you had in Perl over the treatmentof warnings in your program was through either the <tt class="userinput"><b>-w</b></tt> command-lineoption or the <tt class="literal">$^W</tt> variable. Although useful, these tend to be all-or-nothing affairs. The <tt class="userinput"><b>-w</b></tt> option ends up enabling warningsin pieces of module code that you may not have written, which isoccasionally problematic for you and embarrassing for the originalauthor. Using <tt class="literal">$^W</tt> to either disable or enable blocks of codecan be less than optimal because it works only during executiontime, not during compile time.<a href="#FOOTNOTE-3">[3]</a> Another issue is that this program-wide globalvariable is scoped dynamically, not lexically. That means that ifyou enable it in a block and then from there call other code, youagain risk enabling warnings in code not developed with such exactingstandards in mind.</p><blockquote class="footnote"><a name="FOOTNOTE-3"></a><p>[3]In the absence of <tt class="literal">BEGIN</tt> blocks, ofcourse.</p></blockquote><p>The <tt class="literal">warnings</tt> pragma circumvents these limitations by being alexically scoped, compile-time mechanism that permits finer controlover where warnings can or can't be triggered. A hierarchy of warningcategories (see <a href="ch31_22.htm#perl3-warnings">Figure 31-1</a>) has been defined to allow groups of warnings to be enabledor disabled in isolation from one another. (The exact categorizationis experimental and subject to change.) These categories can be combined by passing multiple arguments to <tt class="literal">use</tt> or<tt class="literal">no</tt>:</p><blockquote><pre class="programlisting">use warnings qw(void redefine);no warnings qw(io syntax untie);</pre></blockquote><a name="perl3-warnings"></a><div class="figure"></div><h4 class="objtitle">Figure 31.1. Perl's warning categories</h4><p>If multiple instances of the <tt class="literal">warnings</tt> pragma are active for agiven scope, their effects are cumulative:<blockquote><pre class="programlisting">use warnings "void"; # Only "void" warnings enabled....use warnings "io"; # Both "void" and "io" warnings now enabled....no warnings "void"; # Only "io" warnings now enabled.</pre></blockquote>To make fatal errors of all warnings enabled by a particular<tt class="literal">warnings</tt> pragma, use the word <tt class="literal">FATAL</tt> at the front of the importlist. This is useful when you would prefer a certain condition thatnormally causes only a warning to abort your program. Suppose, forexample, that you considered it so improper to use an invalid string asa number (which normally produces a value of 0) that you want thisbrazen act to kill your program. While you're at it, you decide thatusing uninitialized values in places where real string or numericvalues are expected should also be cause for immediate suicide:<blockquote><pre class="programlisting">{ use warnings FATAL => qw(numeric uninitialized); $x = $y + $z;}</pre></blockquote>Now if either <tt class="literal">$y</tt> or <tt class="literal">$z</tt> is uninitialized (that is, holds the specialscalar value, <tt class="literal">undef</tt>), or if they contain strings that don'tcleanly convert into numeric values, instead of going merrily onits way, or at most issuing a small complaint if you had <tt class="userinput"><b>-w</b></tt> enabled,your program will now raise a exception. (Think of this as Perlrunning in Python mode.) If you aren't trapping exceptions, thatmakes it a fatal error. The exception text is the same as wouldnormally appear in the warning message.</p><p>The <tt class="literal">warnings</tt> pragma ignores the <tt class="userinput"><b>-w</b></tt> command-line switch andthe value of the <tt class="literal">$^W</tt> variable; the pragma's settings take precedence.However, the <tt class="userinput"><b>-W</b></tt> command-line flag overrides thepragma, enabling full warnings in all code within your program,even code loaded with <tt class="literal">do</tt>, <tt class="literal">require</tt>, or <tt class="literal">use</tt>. In other words,with <tt class="userinput"><b>-W</b></tt>, Perl pretends that every block in your program has a<tt class="literal">use warnings 'all'</tt> pragma. Think of it as a <em class="emphasis">lint</em>(1) for Perlprograms. (But see also the online documentation for the <tt class="literal">B::Lint</tt> module.)The <tt class="userinput"><b>-X</b></tt> command-line flag works the other way around. It pretendsthat every block has <tt class="literal">no warnings 'all'</tt> in effect.</p><p>Several functions are provided to assist module authors who want tomake their module's functions behave like built-in functions withrespect to the lexical scoping of the caller (that is, so that users ofthe module can lexically enable or disable warnings the module mightissue):</p><dl><dt><b><tt class="literal">warnings::register</tt></b></dt><dd><p>Registers the current module name as a new category of warnings, sothat users of your module can turn off warnings from it.</p></dd><dt><b><tt class="literal">warnings::enabled(</tt><em class="replaceable">CATEGORY</em><tt class="literal">)</tt></b></dt><dd><p>Returns true if the warnings category <em class="replaceable">CATEGORY</em> is enabled in thelexical scope of the calling module. Otherwise, it returns false.If <em class="replaceable">CATEGORY</em> is not supplied, the current package name is used.</p></dd><dt><b><tt class="literal">warnings::warn(</tt><em class="replaceable">CATEGORY</em>, <em class="replaceable">MESSAGE</em><tt class="literal">)</tt></b></dt><dd><p>If the calling module has <em class="emphasis">not</em> set <em class="replaceable">CATEGORY</em>to "<tt class="literal">FATAL</tt>", prints<em class="replaceable">MESSAGE</em> to <tt class="literal">STDERR</tt>. If the calling module has set <em class="replaceable">CATEGORY</em> to"<tt class="literal">FATAL</tt>", prints <em class="replaceable">MESSAGE</em> to <tt class="literal">STDERR</tt>, then dies. If <em class="replaceable">CATEGORY</em>is not supplied, the current package name is used.</p></dd></dl><p></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_21.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch32_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.21. use vars</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">32. Standard Modules</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -