perllexwarn.html
来自「perl教程」· HTML 代码 · 共 587 行 · 第 1/3 页
HTML
587 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perllexwarn - Perl Lexical Warnings</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perllexwarn - Perl Lexical Warnings</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#default_warnings_and_optional_warnings">Default Warnings and Optional Warnings</a></li>
<li><a href="#what_s_wrong_with_w_and___w">What's wrong with <strong>-w</strong> and <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a></a></li>
<li><a href="#controlling_warnings_from_the_command_line">Controlling Warnings from the Command Line</a></li>
<li><a href="#backward_compatibility">Backward Compatibility</a></li>
<li><a href="#category_hierarchy">Category Hierarchy</a></li>
<li><a href="#fatal_warnings">Fatal Warnings</a></li>
<li><a href="#reporting_warnings_from_a_module">Reporting Warnings from a Module</a></li>
</ul>
<li><a href="#todo">TODO</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author">AUTHOR</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perllexwarn - Perl Lexical Warnings</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The <code>use warnings</code> pragma is a replacement for both the command line
flag <strong>-w</strong> and the equivalent Perl variable, <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a>.</p>
<p>The pragma works just like the existing "strict" pragma.
This means that the scope of the warning pragma is limited to the
enclosing block. It also means that the pragma setting will not
leak across files (via <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>, <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> or <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>). This allows
authors to independently define the degree of warning checks that will
be applied to their module.</p>
<p>By default, optional warnings are disabled, so any legacy code that
doesn't attempt to control the warnings will work unchanged.</p>
<p>All warnings are enabled in a block by either of these:</p>
<pre>
<span class="keyword">use</span> <span class="variable">warnings</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">warnings</span> <span class="string">'all'</span><span class="operator">;</span>
</pre>
<p>Similarly all warnings are disabled in a block by either of these:</p>
<pre>
<span class="keyword">no</span> <span class="variable">warnings</span><span class="operator">;</span>
<span class="keyword">no</span> <span class="variable">warnings</span> <span class="string">'all'</span><span class="operator">;</span>
</pre>
<p>For example, consider the code below:</p>
<pre>
<span class="keyword">use</span> <span class="variable">warnings</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">@a</span><span class="operator">;</span>
<span class="operator">{</span>
<span class="keyword">no</span> <span class="variable">warnings</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$b</span> <span class="operator">=</span> <span class="variable">@a</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="keyword">my</span> <span class="variable">$c</span> <span class="operator">=</span> <span class="variable">@a</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">;</span>
</pre>
<p>The code in the enclosing block has warnings enabled, but the inner
block has them disabled. In this case that means the assignment to the
scalar <code>$c</code> will trip the <code>"Scalar value @a[0] better written as $a[0]"</code>
warning, but the assignment to the scalar <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> will not.</p>
<p>
</p>
<h2><a name="default_warnings_and_optional_warnings">Default Warnings and Optional Warnings</a></h2>
<p>Before the introduction of lexical warnings, Perl had two classes of
warnings: mandatory and optional.</p>
<p>As its name suggests, if your code tripped a mandatory warning, you
would get a warning whether you wanted it or not.
For example, the code below would always produce an <code>"isn't numeric"</code>
warning about the "2:".</p>
<pre>
<span class="keyword">my</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="string">"2:"</span> <span class="operator">+</span> <span class="number">3</span><span class="operator">;</span>
</pre>
<p>With the introduction of lexical warnings, mandatory warnings now become
<em>default</em> warnings. The difference is that although the previously
mandatory warnings are still enabled by default, they can then be
subsequently enabled or disabled with the lexical warning pragma. For
example, in the code below, an <code>"isn't numeric"</code> warning will only
be reported for the <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> variable.</p>
<pre>
<span class="keyword">my</span> <span class="variable">$a</span> <span class="operator">=</span> <span class="string">"2:"</span> <span class="operator">+</span> <span class="number">3</span><span class="operator">;</span>
<span class="keyword">no</span> <span class="variable">warnings</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$b</span> <span class="operator">=</span> <span class="string">"2:"</span> <span class="operator">+</span> <span class="number">3</span><span class="operator">;</span>
</pre>
<p>Note that neither the <strong>-w</strong> flag or the <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a> can be used to
disable/enable default warnings. They are still mandatory in this case.</p>
<p>
</p>
<h2><a name="what_s_wrong_with_w_and___w">What's wrong with <strong>-w</strong> and <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a></a></h2>
<p>Although very useful, the big problem with using <strong>-w</strong> on the command
line to enable warnings is that it is all or nothing. Take the typical
scenario when you are writing a Perl program. Parts of the code you
will write yourself, but it's very likely that you will make use of
pre-written Perl modules. If you use the <strong>-w</strong> flag in this case, you
end up enabling warnings in pieces of code that you haven't written.</p>
<p>Similarly, using <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a> to either disable or enable blocks of code is
fundamentally flawed. For a start, say you want to disable warnings in
a block of code. You might expect this to be enough to do the trick:</p>
<pre>
<span class="operator">{</span>
<span class="keyword">local</span> <span class="operator">(</span><span class="variable">$^W</span><span class="operator">)</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$a</span> <span class="operator">=+</span> <span class="number">2</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$b</span><span class="operator">;</span> <span class="keyword">chop</span> <span class="variable">$b</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>When this code is run with the <strong>-w</strong> flag, a warning will be produced
for the <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> line -- <code>"Reversed += operator"</code>.</p>
<p>The problem is that Perl has both compile-time and run-time warnings. To
disable compile-time warnings you need to rewrite the code like this:</p>
<pre>
<span class="operator">{</span>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="variable">$^W</span> <span class="operator">=</span> <span class="number">0</span> <span class="operator">}</span>
<span class="keyword">my</span> <span class="variable">$a</span> <span class="operator">=+</span> <span class="number">2</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$b</span><span class="operator">;</span> <span class="keyword">chop</span> <span class="variable">$b</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>The other big problem with <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a> is the way you can inadvertently
change the warning setting in unexpected places in your code. For example,
when the code below is run (without the <strong>-w</strong> flag), the second call
to <code>doit</code> will trip a <code>"Use of uninitialized value"</code> warning, whereas
the first will not.</p>
<pre>
<span class="keyword">sub</span><span class="variable"> doit
</span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$b</span><span class="operator">;</span> <span class="keyword">chop</span> <span class="variable">$b</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">doit</span><span class="operator">();</span>
</pre>
<pre>
<span class="operator">{</span>
<span class="keyword">local</span> <span class="operator">(</span><span class="variable">$^W</span><span class="operator">)</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="variable">doit</span><span class="operator">()</span>
<span class="operator">}</span>
</pre>
<p>This is a side-effect of <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a> being dynamically scoped.</p>
<p>Lexical warnings get around these limitations by allowing finer control
over where warnings can or can't be tripped.</p>
<p>
</p>
<h2><a name="controlling_warnings_from_the_command_line">Controlling Warnings from the Command Line</a></h2>
<p>There are three Command Line flags that can be used to control when
warnings are (or aren't) produced:</p>
<dl>
<dt><strong><a name="item__2dw"><strong>-w</strong></a></strong>
<dd>
<p>This is the existing flag. If the lexical warnings pragma is <strong>not</strong>
used in any of you code, or any of the modules that you use, this flag
will enable warnings everywhere. See <a href="#backward_compatibility">Backward Compatibility</a> for
details of how this flag interacts with lexical warnings.</p>
</dd>
</li>
<dt><strong><a name="item__2dw"><strong>-W</strong></a></strong>
<dd>
<p>If the <strong>-W</strong> flag is used on the command line, it will enable all warnings
throughout the program regardless of whether warnings were disabled
locally using <code>no warnings</code> or <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W =0</code></a>. This includes all files that get
included via <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>, <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> or <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>.
Think of it as the Perl equivalent of the "lint" command.</p>
</dd>
</li>
<dt><strong><a name="item__2dx"><strong>-X</strong></a></strong>
<dd>
<p>Does the exact opposite to the <strong>-W</strong> flag, i.e. it disables all warnings.</p>
</dd>
</li>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?