⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch31_08.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>use diagnostics (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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="use diagnostics"><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_07.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="ch31_09.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.8. use diagnostics</h2><blockquote><pre class="programlisting">use diagnostics;            # compile-time enableuse diagnostics -verbose;enable  diagnostics;        # run-time enabledisable diagnostics;        # run-time disable</pre></blockquote><p>This pragma expands the normal, terse diagnostics and suppresses duplicatewarnings.  It augments the short versions with the more explicative andendearing descriptions found in <a href="ch33_01.htm">Chapter 33, "Diagnostic Messages"</a>.Like other pragmas, it also affects the compilation phase of your program,not just the run phase.</p><p>When you <tt class="literal">use diagnostics</tt> at the start of your program, thisautomatically enables Perl's <tt class="userinput"><b>-w</b></tt> command-line switch by setting<tt class="literal">$^W</tt> to 1.  The remainder of your whole compilation will then besubject to enhanced diagnostics.  These still go out on <tt class="literal">STDERR</tt>.</p><p>Because of the interaction between run-time and compile-time issues,and because it's probably not a good idea anyway, you may not use <tt class="literal">nodiagnostics</tt> to turn them off at compile time.  However, you maycontrol their behavior at run time using the <tt class="literal">disable</tt> and <tt class="literal">enable</tt>methods. (Make sure you do the <tt class="literal">use</tt> first,or else you won't be able to get at the methods.)</p><p>The <tt class="literal">-verbose</tt> flag first prints out the <em class="emphasis">perldiag</em> manpage'sintroduction before any other diagnostics are issued.  The<tt class="literal">$diagnostics::PRETTY</tt> variable can be set (before the <tt class="literal">use</tt>) togenerate nicer escape sequences for pagers like <em class="emphasis">less</em>(1) or<em class="emphasis">more</em>(1):<blockquote><pre class="programlisting">BEGIN { $diagnostics::PRETTY = 1 } use diagnostics;</pre></blockquote>Warnings dispatched from Perl and detected by this pragma are eachdisplayed only once.  This is useful when you're caught in a loopthat's generating the same warning (like uninitialized value) over andover again.  Manually generated warnings, such as those stemmingfrom calls to <tt class="literal">warn</tt> or <tt class="literal">carp</tt>, are unaffected by this duplicatedetection mechanism.</p><p>Here are some examples of using the <tt class="literal">diagnostics</tt> pragma.  Thefollowing file is certain to trigger a few errors at both run timeand compile time:<blockquote><pre class="programlisting">use diagnostics;print NOWHERE "nothing\n";print STDERR "\n\tThis message should be unadorned.\n";warn "\tThis is a user warning";print "\nDIAGNOSTIC TESTER: Please enter a &lt;CR&gt; here: ";my $a, $b = scalar &lt;STDIN&gt;;print "\n";print $x/$y;</pre></blockquote>Here's the output:<blockquote><pre class="programlisting">Parentheses missing around "my" list at diagtest line 6 (#1)            (W parenthesis) You said something like                my $foo, $bar = @_;            when you meant                my ($foo, $bar) = @_;Remember that "my", "our", and "local" bind tighter than comma.Name "main::NOWHERE" used only once: possible typo at diagtest line 2 (#2)(W once) Typographical errors often show up as unique variable    names.  If you had a good reason for having a unique name,    then just mention it again somehow to suppress the message.    The our declaration is provided for this purpose.        Name "main::b" used only once: possible typo at diagtest line 6 (#2)Name "main::x" used only once: possible typo at diagtest line 8 (#2)Name "main::y" used only once: possible typo at diagtest line 8 (#2)Filehandle main::NOWHERE never opened at diagtest line 2 (#3)(W unopened) An I/O operation was attempted on a filehandle that    was never initialized.  You need to do an open() or a socket()    call, or call a constructor from the FileHandle package.                This message should be unadorned.        This is a user warning at diagtest line 4.DIAGNOSTIC TESTER: Please enter a &lt;CR&gt; here: Use of uninitialized value in division (/) at diagtest line 8 (#4)(W uninitialized) An undefined value was used as if it were    already defined.  It was interpreted as a "" or a 0, but maybe    it was a mistake.  To suppress this warning assign a defined    value to your variables.        Illegal division by zero at diagtest line 8 (#5)(F) You tried to divide a number by 0.  Either something was    wrong in your logic, or you need to put a conditional in to    guard against meaningless input.        Uncaught exception from user code:        Illegal division by zero at diagtest line 8.</pre></blockquote></p><p>Diagnostic messages derive from the <em class="emphasis">perldiag.pod</em> file.  If anextant <tt class="literal">$SIG{__WARN__}</tt> handler is discovered, this will stillbe honored, but only after the <tt class="literal">diagnostics::splainthis</tt> function(the pragma's <tt class="literal">$SIG{__WARN__}</tt> interceptor) has had its way withyour warnings.  Perl does not currently support stacked handlers,so this is the best we can do for now.  There is a <tt class="literal">$diagnostics::DEBUG</tt>variable you may set if you're desperately curious about what sorts ofthings are being intercepted:<blockquote><pre class="programlisting">BEGIN { $diagnostics::DEBUG = 1 } use diagnostics;</pre></blockquote></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_07.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="ch31_09.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.7. use constant</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">31.9. use fields</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 &copy; 2001</a> O'Reilly &amp; 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 + -