1PERLLEXWARN(1)         Perl Programmers Reference Guide         PERLLEXWARN(1)
2
3
4

NAME

6       perllexwarn - Perl Lexical Warnings
7

DESCRIPTION

9       The "use warnings" pragma is a replacement for both the command line
10       flag -w and the equivalent Perl variable, $^W.
11
12       The pragma works just like the existing "strict" pragma.  This means
13       that the scope of the warning pragma is limited to the enclosing block.
14       It also means that the pragma setting will not leak across files (via
15       "use", "require" or "do"). This allows authors to independently define
16       the degree of warning checks that will be applied to their module.
17
18       By default, optional warnings are disabled, so any legacy code that
19       doesn't attempt to control the warnings will work unchanged.
20
21       All warnings are enabled in a block by either of these:
22
23           use warnings;
24           use warnings 'all';
25
26       Similarly all warnings are disabled in a block by either of these:
27
28           no warnings;
29           no warnings 'all';
30
31       For example, consider the code below:
32
33           use warnings;
34           my @a;
35           {
36               no warnings;
37               my $b = @a[0];
38           }
39           my $c = @a[0];
40
41       The code in the enclosing block has warnings enabled, but the inner
42       block has them disabled. In this case that means the assignment to the
43       scalar $c will trip the "Scalar value @a[0] better written as $a[0]"
44       warning, but the assignment to the scalar $b will not.
45
46       Default Warnings and Optional Warnings
47
48       Before the introduction of lexical warnings, Perl had two classes of
49       warnings: mandatory and optional.
50
51       As its name suggests, if your code tripped a mandatory warning, you
52       would get a warning whether you wanted it or not.  For example, the
53       code below would always produce an "isn't numeric" warning about the
54       "2:".
55
56           my $a = "2:" + 3;
57
58       With the introduction of lexical warnings, mandatory warnings now
59       become default warnings. The difference is that although the previously
60       mandatory warnings are still enabled by default, they can then be sub‐
61       sequently enabled or disabled with the lexical warning pragma. For
62       example, in the code below, an "isn't numeric" warning will only be
63       reported for the $a variable.
64
65           my $a = "2:" + 3;
66           no warnings;
67           my $b = "2:" + 3;
68
69       Note that neither the -w flag or the $^W can be used to disable/enable
70       default warnings. They are still mandatory in this case.
71
72       What's wrong with -w and $^W
73
74       Although very useful, the big problem with using -w on the command line
75       to enable warnings is that it is all or nothing. Take the typical sce‐
76       nario when you are writing a Perl program. Parts of the code you will
77       write yourself, but it's very likely that you will make use of pre-
78       written Perl modules. If you use the -w flag in this case, you end up
79       enabling warnings in pieces of code that you haven't written.
80
81       Similarly, using $^W to either disable or enable blocks of code is fun‐
82       damentally flawed. For a start, say you want to disable warnings in a
83       block of code. You might expect this to be enough to do the trick:
84
85            {
86                local ($^W) = 0;
87                my $a =+ 2;
88                my $b; chop $b;
89            }
90
91       When this code is run with the -w flag, a warning will be produced for
92       the $a line -- "Reversed += operator".
93
94       The problem is that Perl has both compile-time and run-time warnings.
95       To disable compile-time warnings you need to rewrite the code like
96       this:
97
98            {
99                BEGIN { $^W = 0 }
100                my $a =+ 2;
101                my $b; chop $b;
102            }
103
104       The other big problem with $^W is the way you can inadvertently change
105       the warning setting in unexpected places in your code. For example,
106       when the code below is run (without the -w flag), the second call to
107       "doit" will trip a "Use of uninitialized value" warning, whereas the
108       first will not.
109
110           sub doit
111           {
112               my $b; chop $b;
113           }
114
115           doit();
116
117           {
118               local ($^W) = 1;
119               doit()
120           }
121
122       This is a side-effect of $^W being dynamically scoped.
123
124       Lexical warnings get around these limitations by allowing finer control
125       over where warnings can or can't be tripped.
126
127       Controlling Warnings from the Command Line
128
129       There are three Command Line flags that can be used to control when
130       warnings are (or aren't) produced:
131
132       -w   This is  the existing flag. If the lexical warnings pragma is not
133            used in any of you code, or any of the modules that you use, this
134            flag will enable warnings everywhere. See "Backward Compatibility"
135            for details of how this flag interacts with lexical warnings.
136
137       -W   If the -W flag is used on the command line, it will enable all
138            warnings throughout the program regardless of whether warnings
139            were disabled locally using "no warnings" or "$^W =0". This
140            includes all files that get included via "use", "require" or "do".
141            Think of it as the Perl equivalent of the "lint" command.
142
143       -X   Does the exact opposite to the -W flag, i.e. it disables all warn‐
144            ings.
145
146       Backward Compatibility
147
148       If you are used with working with a version of Perl prior to the intro‐
149       duction of lexically scoped warnings, or have code that uses both lexi‐
150       cal warnings and $^W, this section will describe how they interact.
151
152       How Lexical Warnings interact with -w/$^W:
153
154       1.   If none of the three command line flags (-w, -W or -X) that con‐
155            trol warnings is used and neither $^W or the "warnings" pragma are
156            used, then default warnings will be enabled and optional warnings
157            disabled.  This means that legacy code that doesn't attempt to
158            control the warnings will work unchanged.
159
160       2.   The -w flag just sets the global $^W variable as in 5.005 -- this
161            means that any legacy code that currently relies on manipulating
162            $^W to control warning behavior will still work as is.
163
164       3.   Apart from now being a boolean, the $^W variable operates in
165            exactly the same horrible uncontrolled global way, except that it
166            cannot disable/enable default warnings.
167
168       4.   If a piece of code is under the control of the "warnings" pragma,
169            both the $^W variable and the -w flag will be ignored for the
170            scope of the lexical warning.
171
172       5.   The only way to override a lexical warnings setting is with the -W
173            or -X command line flags.
174
175       The combined effect of 3 & 4 is that it will allow code which uses the
176       "warnings" pragma to control the warning behavior of $^W-type code
177       (using a "local $^W=0") if it really wants to, but not vice-versa.
178
179       Category Hierarchy
180
181       A hierarchy of "categories" have been defined to allow groups of warn‐
182       ings to be enabled/disabled in isolation.
183
184       The current hierarchy is:
185
186         all -+
187
188              +- closure
189
190              +- deprecated
191
192              +- exiting
193
194              +- glob
195
196              +- io -----------+
197              ⎪                ⎪
198              ⎪                +- closed
199              ⎪                ⎪
200              ⎪                +- exec
201              ⎪                ⎪
202              ⎪                +- layer
203              ⎪                ⎪
204              ⎪                +- newline
205              ⎪                ⎪
206              ⎪                +- pipe
207              ⎪                ⎪
208              ⎪                +- unopened
209
210              +- misc
211
212              +- numeric
213
214              +- once
215
216              +- overflow
217
218              +- pack
219
220              +- portable
221
222              +- recursion
223
224              +- redefine
225
226              +- regexp
227
228              +- severe -------+
229              ⎪                ⎪
230              ⎪                +- debugging
231              ⎪                ⎪
232              ⎪                +- inplace
233              ⎪                ⎪
234              ⎪                +- internal
235              ⎪                ⎪
236              ⎪                +- malloc
237
238              +- signal
239
240              +- substr
241
242              +- syntax -------+
243              ⎪                ⎪
244              ⎪                +- ambiguous
245              ⎪                ⎪
246              ⎪                +- bareword
247              ⎪                ⎪
248              ⎪                +- digit
249              ⎪                ⎪
250              ⎪                +- parenthesis
251              ⎪                ⎪
252              ⎪                +- precedence
253              ⎪                ⎪
254              ⎪                +- printf
255              ⎪                ⎪
256              ⎪                +- prototype
257              ⎪                ⎪
258              ⎪                +- qw
259              ⎪                ⎪
260              ⎪                +- reserved
261              ⎪                ⎪
262              ⎪                +- semicolon
263
264              +- taint
265
266              +- threads
267
268              +- uninitialized
269
270              +- unpack
271
272              +- untie
273
274              +- utf8
275
276              +- void
277
278              +- y2k
279
280       Just like the "strict" pragma any of these categories can be combined
281
282           use warnings qw(void redefine);
283           no warnings qw(io syntax untie);
284
285       Also like the "strict" pragma, if there is more than one instance of
286       the "warnings" pragma in a given scope the cumulative effect is addi‐
287       tive.
288
289           use warnings qw(void); # only "void" warnings enabled
290           ...
291           use warnings qw(io);   # only "void" & "io" warnings enabled
292           ...
293           no warnings qw(void);  # only "io" warnings enabled
294
295       To determine which category a specific warning has been assigned to see
296       perldiag.
297
298       Note: In Perl 5.6.1, the lexical warnings category "deprecated" was a
299       sub-category of the "syntax" category. It is now a top-level category
300       in its own right.
301
302       Fatal Warnings
303
304       The presence of the word "FATAL" in the category list will escalate any
305       warnings detected from the categories specified in the lexical scope
306       into fatal errors. In the code below, the use of "time", "length" and
307       "join" can all produce a "Useless use of xxx in void context" warning.
308
309           use warnings;
310
311           time;
312
313           {
314               use warnings FATAL => qw(void);
315               length "abc";
316           }
317
318           join "", 1,2,3;
319
320           print "done\n";
321
322       When run it produces this output
323
324           Useless use of time in void context at fatal line 3.
325           Useless use of length in void context at fatal line 7.
326
327       The scope where "length" is used has escalated the "void" warnings cat‐
328       egory into a fatal error, so the program terminates immediately it
329       encounters the warning.
330
331       To explicitly turn off a "FATAL" warning you just disable the warning
332       it is associated with.  So, for example, to disable the "void" warning
333       in the example above, either of these will do the trick:
334
335           no warnings qw(void);
336           no warnings FATAL => qw(void);
337
338       If you want to downgrade a warning that has been escalated into a fatal
339       error back to a normal warning, you can use the "NONFATAL" keyword. For
340       example, the code below will promote all warnings into fatal errors,
341       except for those in the "syntax" category.
342
343           use warnings FATAL => 'all', NONFATAL => 'syntax';
344
345       Reporting Warnings from a Module
346
347       The "warnings" pragma provides a number of functions that are useful
348       for module authors. These are used when you want to report a module-
349       specific warning to a calling module has enabled warnings via the
350       "warnings" pragma.
351
352       Consider the module "MyMod::Abc" below.
353
354           package MyMod::Abc;
355
356           use warnings::register;
357
358           sub open {
359               my $path = shift;
360               if ($path !~ m#^/#) {
361                   warnings::warn("changing relative path to /var/abc")
362                       if warnings::enabled();
363                   $path = "/var/abc/$path";
364               }
365           }
366
367           1;
368
369       The call to "warnings::register" will create a new warnings category
370       called "MyMod::abc", i.e. the new category name matches the current
371       package name. The "open" function in the module will display a warning
372       message if it gets given a relative path as a parameter. This warnings
373       will only be displayed if the code that uses "MyMod::Abc" has actually
374       enabled them with the "warnings" pragma like below.
375
376           use MyMod::Abc;
377           use warnings 'MyMod::Abc';
378           ...
379           abc::open("../fred.txt");
380
381       It is also possible to test whether the pre-defined warnings categories
382       are set in the calling module with the "warnings::enabled" function.
383       Consider this snippet of code:
384
385           package MyMod::Abc;
386
387           sub open {
388               warnings::warnif("deprecated",
389                                "open is deprecated, use new instead");
390               new(@_);
391           }
392
393           sub new
394           ...
395           1;
396
397       The function "open" has been deprecated, so code has been included to
398       display a warning message whenever the calling module has (at least)
399       the "deprecated" warnings category enabled. Something like this, say.
400
401           use warnings 'deprecated';
402           use MyMod::Abc;
403           ...
404           MyMod::Abc::open($filename);
405
406       Either the "warnings::warn" or "warnings::warnif" function should be
407       used to actually display the warnings message. This is because they can
408       make use of the feature that allows warnings to be escalated into fatal
409       errors. So in this case
410
411           use MyMod::Abc;
412           use warnings FATAL => 'MyMod::Abc';
413           ...
414           MyMod::Abc::open('../fred.txt');
415
416       the "warnings::warnif" function will detect this and die after display‐
417       ing the warning message.
418
419       The three warnings functions, "warnings::warn", "warnings::warnif" and
420       "warnings::enabled" can optionally take an object reference in place of
421       a category name. In this case the functions will use the class name of
422       the object as the warnings category.
423
424       Consider this example:
425
426           package Original;
427
428           no warnings;
429           use warnings::register;
430
431           sub new
432           {
433               my $class = shift;
434               bless [], $class;
435           }
436
437           sub check
438           {
439               my $self = shift;
440               my $value = shift;
441
442               if ($value % 2 && warnings::enabled($self))
443                 { warnings::warn($self, "Odd numbers are unsafe") }
444           }
445
446           sub doit
447           {
448               my $self = shift;
449               my $value = shift;
450               $self->check($value);
451               # ...
452           }
453
454           1;
455
456           package Derived;
457
458           use warnings::register;
459           use Original;
460           our @ISA = qw( Original );
461           sub new
462           {
463               my $class = shift;
464               bless [], $class;
465           }
466
467           1;
468
469       The code below makes use of both modules, but it only enables warnings
470       from "Derived".
471
472           use Original;
473           use Derived;
474           use warnings 'Derived';
475           my $a = new Original;
476           $a->doit(1);
477           my $b = new Derived;
478           $a->doit(1);
479
480       When this code is run only the "Derived" object, $b, will generate a
481       warning.
482
483           Odd numbers are unsafe at main.pl line 7
484
485       Notice also that the warning is reported at the line where the object
486       is first used.
487

TODO

489         perl5db.pl
490           The debugger saves and restores C<$^W> at runtime. I haven't checked
491           whether the debugger will still work with the lexical warnings
492           patch applied.
493
494         diagnostics.pm
495           I *think* I've got diagnostics to work with the lexical warnings
496           patch, but there were design decisions made in diagnostics to work
497           around the limitations of C<$^W>. Now that those limitations are gone,
498           the module should be revisited.
499
500         document calling the warnings::* functions from XS
501

SEE ALSO

503       warnings, perldiag.
504

AUTHOR

506       Paul Marquess
507
508
509
510perl v5.8.8                       2006-01-07                    PERLLEXWARN(1)
Impressum