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">
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:" />
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"> perllexwarn - Perl Lexical Warnings
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#description">DESCRIPTION
</a></li>
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>
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>
44 <h1><a name=
"name_x_warning__lexical__x_warnings__x_warning_">NAME
46 <p>perllexwarn - Perl Lexical Warnings
</p>
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>
64 use warnings 'all';
</pre>
65 <p>Similarly all warnings are disabled in a block by either of these:
</p>
68 no warnings 'all';
</pre>
69 <p>For example, consider the code below:
</p>
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>"Scalar value @a[
0] better written as $a[
0]
"</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>
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>"isn't numeric
"</code>
90 warning about the ``
2:''.
</p>
92 my $a =
"2:
" +
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>"isn't numeric
"</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>
100 my $a =
"2:
" +
3;
102 my $b =
"2:
" +
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>
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>
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>"Reversed += operator
"</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>
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>"Use of uninitialized value
"</code> warning, whereas
137 the first will not.
</p>
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>
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>
159 <dt><strong><a name=
"item__2dw_x_3c_2dw_3e"><strong>-w
</strong>
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>
169 <dt><strong><a name=
"item__2dw_x_3c_2dw_3e"><strong>-W
</strong>
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>
180 <dt><strong><a name=
"item__2dx_x_3c_2dx_3e"><strong>-X
</strong>
184 <p>Does the exact opposite to the
<strong>-W
</strong> flag, i.e. it disables all warnings.
</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>
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
201 This means that legacy code that doesn't attempt to control the warnings
202 will work unchanged.
</p>
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>
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>
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>
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>
224 <p>The combined effect of
3 & 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>
229 <h2><a name=
"category_hierarchy_x_warning__categories_">Category Hierarchy
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>
328 <p>Just like the ``strict'' pragma any of these categories can be combined
</p>
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>
335 use warnings qw(void); # only
"void
" warnings enabled
337 use warnings qw(io); # only
"void
" & "io
" warnings enabled
339 no warnings qw(void); # only
"io
" 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>
347 <h2><a name=
"fatal_warnings_x_warning__fatal_">Fatal Warnings
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>"Useless use of xxx in void context
"</code>
360 use warnings FATAL =
> qw(void);
361 length
"abc
";
364 join
"",
1,
2,
3;
</pre>
366 print
"done\n
";
</pre>
367 <p>When run it produces this output
</p>
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>
378 no warnings qw(void);
379 no warnings FATAL =
> 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>
385 use warnings FATAL =
> 'all', NONFATAL =
> 'syntax';
</pre>
388 <h2><a name=
"reporting_warnings_from_a_module_x_warning__reporting__x_warning__registering_">Reporting Warnings from a Module
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>
394 <p>Consider the module
<code>MyMod::Abc
</code> below.
</p>
396 package MyMod::Abc;
</pre>
398 use warnings::register;
</pre>
402 if ($path !~ m#^/#) {
403 warnings::warn(
"changing relative path to /var/abc
")
404 if warnings::enabled();
405 $path =
"/var/abc/$path
";
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>
418 use warnings 'MyMod::Abc';
420 abc::open(
"../fred.txt
");
</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>
425 package MyMod::Abc;
</pre>
428 warnings::warnif(
"deprecated
",
429 "open is deprecated, use new instead
");
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>
440 use warnings 'deprecated';
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>
450 use warnings FATAL =
> '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>
461 package Original;
</pre>
464 use warnings::register;
</pre>
475 my $value = shift;
</pre>
477 if ($value %
2 && warnings::enabled($self))
478 { warnings::warn($self,
"Odd numbers are unsafe
") }
485 $self-
>check($value);
491 package Derived;
</pre>
493 use warnings::register;
495 our @ISA = qw( Original );
503 <p>The code below makes use of both modules, but it only enables warnings from
504 <code>Derived
</code>.
</p>
508 use warnings 'Derived';
509 my $a = new Original;
512 $a-
>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
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
522 <h1><a name=
"todo">TODO
</a></h1>
525 The debugger saves and restores C
<$^W
> at runtime. I haven't checked
526 whether the debugger will still work with the lexical warnings
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
<$^W
>. Now that those limitations are gone,
533 the module should be revisited.
</pre>
535 document calling the warnings::* functions from XS
</pre>
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>
544 <h1><a name=
"author">AUTHOR
</a></h1>
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"> perllexwarn - Perl Lexical Warnings
</span></strong></big>