Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perllexwarn.html
blob0d8c09fa7208c19046f644bfd6cfb17173cc92f0
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perllexwarn - Perl Lexical Warnings</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perllexwarn - Perl Lexical Warnings</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#default_warnings_and_optional_warnings">Default Warnings and Optional Warnings</a></li>
27 <li><a href="#what_s_wrong_with_w_and___w">What's wrong with <strong>-w</strong> and <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a></a></li>
28 <li><a href="#controlling_warnings_from_the_command_line">Controlling Warnings from the Command Line</a></li>
29 <li><a href="#backward_compatibility">Backward Compatibility</a></li>
30 <li><a href="#category_hierarchy">Category Hierarchy</a></li>
31 <li><a href="#fatal_warnings">Fatal Warnings</a></li>
32 <li><a href="#reporting_warnings_from_a_module">Reporting Warnings from a Module</a></li>
33 </ul>
35 <li><a href="#todo">TODO</a></li>
36 <li><a href="#see_also">SEE ALSO</a></li>
37 <li><a href="#author">AUTHOR</a></li>
38 </ul>
39 <!-- INDEX END -->
41 <hr />
42 <p>
43 </p>
44 <h1><a name="name_x_warning__lexical__x_warnings__x_warning_">NAME
45 </a></h1>
46 <p>perllexwarn - Perl Lexical Warnings</p>
47 <p>
48 </p>
49 <hr />
50 <h1><a name="description">DESCRIPTION</a></h1>
51 <p>The <code>use warnings</code> pragma is a replacement for both the command line
52 flag <strong>-w</strong> and the equivalent Perl variable, <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a>.</p>
53 <p>The pragma works just like the existing ``strict'' pragma.
54 This means that the scope of the warning pragma is limited to the
55 enclosing block. It also means that the pragma setting will not
56 leak across files (via <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> or <code>do</code>). This allows
57 authors to independently define the degree of warning checks that will
58 be applied to their module.</p>
59 <p>By default, optional warnings are disabled, so any legacy code that
60 doesn't attempt to control the warnings will work unchanged.</p>
61 <p>All warnings are enabled in a block by either of these:</p>
62 <pre>
63 use warnings;
64 use warnings 'all';</pre>
65 <p>Similarly all warnings are disabled in a block by either of these:</p>
66 <pre>
67 no warnings;
68 no warnings 'all';</pre>
69 <p>For example, consider the code below:</p>
70 <pre>
71 use warnings;
72 my @a;
74 no warnings;
75 my $b = @a[0];
77 my $c = @a[0];</pre>
78 <p>The code in the enclosing block has warnings enabled, but the inner
79 block has them disabled. In this case that means the assignment to the
80 scalar <code>$c</code> will trip the <code>&quot;Scalar value @a[0] better written as $a[0]&quot;</code>
81 warning, but the assignment to the scalar <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__b"><code>$b</code></a> will not.</p>
82 <p>
83 </p>
84 <h2><a name="default_warnings_and_optional_warnings">Default Warnings and Optional Warnings</a></h2>
85 <p>Before the introduction of lexical warnings, Perl had two classes of
86 warnings: mandatory and optional.</p>
87 <p>As its name suggests, if your code tripped a mandatory warning, you
88 would get a warning whether you wanted it or not.
89 For example, the code below would always produce an <code>&quot;isn't numeric&quot;</code>
90 warning about the ``2:''.</p>
91 <pre>
92 my $a = &quot;2:&quot; + 3;</pre>
93 <p>With the introduction of lexical warnings, mandatory warnings now become
94 <em>default</em> warnings. The difference is that although the previously
95 mandatory warnings are still enabled by default, they can then be
96 subsequently enabled or disabled with the lexical warning pragma. For
97 example, in the code below, an <code>&quot;isn't numeric&quot;</code> warning will only
98 be reported for the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a</code></a> variable.</p>
99 <pre>
100 my $a = &quot;2:&quot; + 3;
101 no warnings;
102 my $b = &quot;2:&quot; + 3;</pre>
103 <p>Note that neither the <strong>-w</strong> flag or the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> can be used to
104 disable/enable default warnings. They are still mandatory in this case.</p>
106 </p>
107 <h2><a name="what_s_wrong_with_w_and___w">What's wrong with <strong>-w</strong> and <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a></a></h2>
108 <p>Although very useful, the big problem with using <strong>-w</strong> on the command
109 line to enable warnings is that it is all or nothing. Take the typical
110 scenario when you are writing a Perl program. Parts of the code you
111 will write yourself, but it's very likely that you will make use of
112 pre-written Perl modules. If you use the <strong>-w</strong> flag in this case, you
113 end up enabling warnings in pieces of code that you haven't written.</p>
114 <p>Similarly, using <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> to either disable or enable blocks of code is
115 fundamentally flawed. For a start, say you want to disable warnings in
116 a block of code. You might expect this to be enough to do the trick:</p>
117 <pre>
119 local ($^W) = 0;
120 my $a =+ 2;
121 my $b; chop $b;
122 }</pre>
123 <p>When this code is run with the <strong>-w</strong> flag, a warning will be produced
124 for the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a</code></a> line -- <code>&quot;Reversed += operator&quot;</code>.</p>
125 <p>The problem is that Perl has both compile-time and run-time warnings. To
126 disable compile-time warnings you need to rewrite the code like this:</p>
127 <pre>
129 BEGIN { $^W = 0 }
130 my $a =+ 2;
131 my $b; chop $b;
132 }</pre>
133 <p>The other big problem with <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> is the way you can inadvertently
134 change the warning setting in unexpected places in your code. For example,
135 when the code below is run (without the <strong>-w</strong> flag), the second call
136 to <code>doit</code> will trip a <code>&quot;Use of uninitialized value&quot;</code> warning, whereas
137 the first will not.</p>
138 <pre>
139 sub doit
141 my $b; chop $b;
142 }</pre>
143 <pre>
144 doit();</pre>
145 <pre>
147 local ($^W) = 1;
148 doit()
149 }</pre>
150 <p>This is a side-effect of <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> being dynamically scoped.</p>
151 <p>Lexical warnings get around these limitations by allowing finer control
152 over where warnings can or can't be tripped.</p>
154 </p>
155 <h2><a name="controlling_warnings_from_the_command_line">Controlling Warnings from the Command Line</a></h2>
156 <p>There are three Command Line flags that can be used to control when
157 warnings are (or aren't) produced:</p>
158 <dl>
159 <dt><strong><a name="item__2dw_x_3c_2dw_3e"><strong>-w</strong>
160 </a></strong>
162 <dd>
163 <p>This is the existing flag. If the lexical warnings pragma is <strong>not</strong>
164 used in any of you code, or any of the modules that you use, this flag
165 will enable warnings everywhere. See <a href="#backward_compatibility">Backward Compatibility</a> for
166 details of how this flag interacts with lexical warnings.</p>
167 </dd>
168 </li>
169 <dt><strong><a name="item__2dw_x_3c_2dw_3e"><strong>-W</strong>
170 </a></strong>
172 <dd>
173 <p>If the <strong>-W</strong> flag is used on the command line, it will enable all warnings
174 throughout the program regardless of whether warnings were disabled
175 locally using <code>no warnings</code> or <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W =0</code></a>. This includes all files that get
176 included via <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> or <code>do</code>.
177 Think of it as the Perl equivalent of the ``lint'' command.</p>
178 </dd>
179 </li>
180 <dt><strong><a name="item__2dx_x_3c_2dx_3e"><strong>-X</strong>
181 </a></strong>
183 <dd>
184 <p>Does the exact opposite to the <strong>-W</strong> flag, i.e. it disables all warnings.</p>
185 </dd>
186 </li>
187 </dl>
189 </p>
190 <h2><a name="backward_compatibility">Backward Compatibility</a></h2>
191 <p>If you are used with working with a version of Perl prior to the
192 introduction of lexically scoped warnings, or have code that uses both
193 lexical warnings and <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a>, this section will describe how they interact.</p>
194 <p>How Lexical Warnings interact with <strong>-w</strong>/<a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a>:</p>
195 <ol>
196 <li>
197 <p>If none of the three command line flags (<strong>-w</strong>, <strong>-W</strong> or <strong>-X</strong>) that
198 control warnings is used and neither <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> or the <code>warnings</code> pragma
199 are used, then default warnings will be enabled and optional warnings
200 disabled.
201 This means that legacy code that doesn't attempt to control the warnings
202 will work unchanged.</p>
203 </li>
204 <li>
205 <p>The <strong>-w</strong> flag just sets the global <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> variable as in 5.005 -- this
206 means that any legacy code that currently relies on manipulating <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a>
207 to control warning behavior will still work as is.</p>
208 </li>
209 <li>
210 <p>Apart from now being a boolean, the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> variable operates in exactly
211 the same horrible uncontrolled global way, except that it cannot
212 disable/enable default warnings.</p>
213 </li>
214 <li>
215 <p>If a piece of code is under the control of the <code>warnings</code> pragma,
216 both the <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item___w"><code>$^W</code></a> variable and the <strong>-w</strong> flag will be ignored for the
217 scope of the lexical warning.</p>
218 </li>
219 <li>
220 <p>The only way to override a lexical warnings setting is with the <strong>-W</strong>
221 or <strong>-X</strong> command line flags.</p>
222 </li>
223 </ol>
224 <p>The combined effect of 3 &amp; 4 is that it will allow code which uses
225 the <code>warnings</code> pragma to control the warning behavior of $^W-type
226 code (using a <code>local $^W=0</code>) if it really wants to, but not vice-versa.</p>
228 </p>
229 <h2><a name="category_hierarchy_x_warning__categories_">Category Hierarchy
230 </a></h2>
231 <p>A hierarchy of ``categories'' have been defined to allow groups of warnings
232 to be enabled/disabled in isolation.</p>
233 <p>The current hierarchy is:</p>
234 <pre>
235 all -+
237 +- closure
239 +- deprecated
241 +- exiting
243 +- glob
245 +- io -----------+
247 | +- closed
249 | +- exec
251 | +- layer
253 | +- newline
255 | +- pipe
257 | +- unopened
259 +- misc
261 +- numeric
263 +- once
265 +- overflow
267 +- pack
269 +- portable
271 +- recursion
273 +- redefine
275 +- regexp
277 +- severe -------+
279 | +- debugging
281 | +- inplace
283 | +- internal
285 | +- malloc
287 +- signal
289 +- substr
291 +- syntax -------+
293 | +- ambiguous
295 | +- bareword
297 | +- digit
299 | +- parenthesis
301 | +- precedence
303 | +- printf
305 | +- prototype
307 | +- qw
309 | +- reserved
311 | +- semicolon
313 +- taint
315 +- threads
317 +- uninitialized
319 +- unpack
321 +- untie
323 +- utf8
325 +- void
327 +- y2k</pre>
328 <p>Just like the ``strict'' pragma any of these categories can be combined</p>
329 <pre>
330 use warnings qw(void redefine);
331 no warnings qw(io syntax untie);</pre>
332 <p>Also like the ``strict'' pragma, if there is more than one instance of the
333 <code>warnings</code> pragma in a given scope the cumulative effect is additive.</p>
334 <pre>
335 use warnings qw(void); # only &quot;void&quot; warnings enabled
337 use warnings qw(io); # only &quot;void&quot; &amp; &quot;io&quot; warnings enabled
339 no warnings qw(void); # only &quot;io&quot; warnings enabled</pre>
340 <p>To determine which category a specific warning has been assigned to see
341 <a href="file://C|\msysgit\mingw\html/pod/perldiag.html">the perldiag manpage</a>.</p>
342 <p>Note: In Perl 5.6.1, the lexical warnings category ``deprecated'' was a
343 sub-category of the ``syntax'' category. It is now a top-level category
344 in its own right.</p>
346 </p>
347 <h2><a name="fatal_warnings_x_warning__fatal_">Fatal Warnings
348 </a></h2>
349 <p>The presence of the word ``FATAL'' in the category list will escalate any
350 warnings detected from the categories specified in the lexical scope
351 into fatal errors. In the code below, the use of <code>time</code>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_length"><code>length</code></a>
352 and <code>join</code> can all produce a <code>&quot;Useless use of xxx in void context&quot;</code>
353 warning.</p>
354 <pre>
355 use warnings;</pre>
356 <pre>
357 time;</pre>
358 <pre>
360 use warnings FATAL =&gt; qw(void);
361 length &quot;abc&quot;;
362 }</pre>
363 <pre>
364 join &quot;&quot;, 1,2,3;</pre>
365 <pre>
366 print &quot;done\n&quot;;</pre>
367 <p>When run it produces this output</p>
368 <pre>
369 Useless use of time in void context at fatal line 3.
370 Useless use of length in void context at fatal line 7.</pre>
371 <p>The scope where <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_length"><code>length</code></a> is used has escalated the <code>void</code> warnings
372 category into a fatal error, so the program terminates immediately it
373 encounters the warning.</p>
374 <p>To explicitly turn off a ``FATAL'' warning you just disable the warning
375 it is associated with. So, for example, to disable the ``void'' warning
376 in the example above, either of these will do the trick:</p>
377 <pre>
378 no warnings qw(void);
379 no warnings FATAL =&gt; qw(void);</pre>
380 <p>If you want to downgrade a warning that has been escalated into a fatal
381 error back to a normal warning, you can use the ``NONFATAL'' keyword. For
382 example, the code below will promote all warnings into fatal errors,
383 except for those in the ``syntax'' category.</p>
384 <pre>
385 use warnings FATAL =&gt; 'all', NONFATAL =&gt; 'syntax';</pre>
387 </p>
388 <h2><a name="reporting_warnings_from_a_module_x_warning__reporting__x_warning__registering_">Reporting Warnings from a Module
389 </a></h2>
390 <p>The <code>warnings</code> pragma provides a number of functions that are useful for
391 module authors. These are used when you want to report a module-specific
392 warning to a calling module has enabled warnings via the <code>warnings</code>
393 pragma.</p>
394 <p>Consider the module <code>MyMod::Abc</code> below.</p>
395 <pre>
396 package MyMod::Abc;</pre>
397 <pre>
398 use warnings::register;</pre>
399 <pre>
400 sub open {
401 my $path = shift;
402 if ($path !~ m#^/#) {
403 warnings::warn(&quot;changing relative path to /var/abc&quot;)
404 if warnings::enabled();
405 $path = &quot;/var/abc/$path&quot;;
407 }</pre>
408 <pre>
409 1;</pre>
410 <p>The call to <code>warnings::register</code> will create a new warnings category
411 called ``MyMod::abc'', i.e. the new category name matches the current
412 package name. The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> function in the module will display a warning
413 message if it gets given a relative path as a parameter. This warnings
414 will only be displayed if the code that uses <code>MyMod::Abc</code> has actually
415 enabled them with the <code>warnings</code> pragma like below.</p>
416 <pre>
417 use MyMod::Abc;
418 use warnings 'MyMod::Abc';
420 abc::open(&quot;../fred.txt&quot;);</pre>
421 <p>It is also possible to test whether the pre-defined warnings categories are
422 set in the calling module with the <code>warnings::enabled</code> function. Consider
423 this snippet of code:</p>
424 <pre>
425 package MyMod::Abc;</pre>
426 <pre>
427 sub open {
428 warnings::warnif(&quot;deprecated&quot;,
429 &quot;open is deprecated, use new instead&quot;);
430 new(@_);
431 }</pre>
432 <pre>
433 sub new
435 1;</pre>
436 <p>The function <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_open"><code>open</code></a> has been deprecated, so code has been included to
437 display a warning message whenever the calling module has (at least) the
438 ``deprecated'' warnings category enabled. Something like this, say.</p>
439 <pre>
440 use warnings 'deprecated';
441 use MyMod::Abc;
443 MyMod::Abc::open($filename);</pre>
444 <p>Either the <code>warnings::warn</code> or <code>warnings::warnif</code> function should be
445 used to actually display the warnings message. This is because they can
446 make use of the feature that allows warnings to be escalated into fatal
447 errors. So in this case</p>
448 <pre>
449 use MyMod::Abc;
450 use warnings FATAL =&gt; 'MyMod::Abc';
452 MyMod::Abc::open('../fred.txt');</pre>
453 <p>the <code>warnings::warnif</code> function will detect this and die after
454 displaying the warning message.</p>
455 <p>The three warnings functions, <code>warnings::warn</code>, <code>warnings::warnif</code>
456 and <code>warnings::enabled</code> can optionally take an object reference in place
457 of a category name. In this case the functions will use the class name
458 of the object as the warnings category.</p>
459 <p>Consider this example:</p>
460 <pre>
461 package Original;</pre>
462 <pre>
463 no warnings;
464 use warnings::register;</pre>
465 <pre>
466 sub new
468 my $class = shift;
469 bless [], $class;
470 }</pre>
471 <pre>
472 sub check
474 my $self = shift;
475 my $value = shift;</pre>
476 <pre>
477 if ($value % 2 &amp;&amp; warnings::enabled($self))
478 { warnings::warn($self, &quot;Odd numbers are unsafe&quot;) }
479 }</pre>
480 <pre>
481 sub doit
483 my $self = shift;
484 my $value = shift;
485 $self-&gt;check($value);
486 # ...
487 }</pre>
488 <pre>
489 1;</pre>
490 <pre>
491 package Derived;</pre>
492 <pre>
493 use warnings::register;
494 use Original;
495 our @ISA = qw( Original );
496 sub new
498 my $class = shift;
499 bless [], $class;
500 }</pre>
501 <pre>
502 1;</pre>
503 <p>The code below makes use of both modules, but it only enables warnings from
504 <code>Derived</code>.</p>
505 <pre>
506 use Original;
507 use Derived;
508 use warnings 'Derived';
509 my $a = new Original;
510 $a-&gt;doit(1);
511 my $b = new Derived;
512 $a-&gt;doit(1);</pre>
513 <p>When this code is run only the <code>Derived</code> object, <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__b"><code>$b</code></a>, will generate
514 a warning.</p>
515 <pre>
516 Odd numbers are unsafe at main.pl line 7</pre>
517 <p>Notice also that the warning is reported at the line where the object is first
518 used.</p>
520 </p>
521 <hr />
522 <h1><a name="todo">TODO</a></h1>
523 <pre>
524 perl5db.pl
525 The debugger saves and restores C&lt;$^W&gt; at runtime. I haven't checked
526 whether the debugger will still work with the lexical warnings
527 patch applied.</pre>
528 <pre>
529 diagnostics.pm
530 I *think* I've got diagnostics to work with the lexical warnings
531 patch, but there were design decisions made in diagnostics to work
532 around the limitations of C&lt;$^W&gt;. Now that those limitations are gone,
533 the module should be revisited.</pre>
534 <pre>
535 document calling the warnings::* functions from XS</pre>
537 </p>
538 <hr />
539 <h1><a name="see_also">SEE ALSO</a></h1>
540 <p><a href="file://C|\msysgit\mingw\html/lib/warnings.html">the warnings manpage</a>, <a href="file://C|\msysgit\mingw\html/pod/perldiag.html">the perldiag manpage</a>.</p>
542 </p>
543 <hr />
544 <h1><a name="author">AUTHOR</a></h1>
545 <p>Paul Marquess</p>
546 <table border="0" width="100%" cellspacing="0" cellpadding="3">
547 <tr><td class="block" style="background-color: #cccccc" valign="middle">
548 <big><strong><span class="block">&nbsp;perllexwarn - Perl Lexical Warnings</span></strong></big>
549 </td></tr>
550 </table>
552 </body>
554 </html>