1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
|
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "warnings 3perl"
.TH warnings 3perl 2024-05-30 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
warnings \- Perl pragma to control optional warnings
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 2
\& use warnings;
\& no warnings;
\&
\& # Standard warnings are enabled by use v5.35 or above
\& use v5.35;
\&
\& use warnings "all";
\& no warnings "uninitialized";
\&
\& # or equivalent to those last two ...
\& use warnings qw(all \-uninitialized);
\&
\& use warnings::register;
\& if (warnings::enabled()) {
\& warnings::warn("some warning");
\& }
\&
\& if (warnings::enabled("void")) {
\& warnings::warn("void", "some warning");
\& }
\&
\& if (warnings::enabled($object)) {
\& warnings::warn($object, "some warning");
\& }
\&
\& warnings::warnif("some warning");
\& warnings::warnif("void", "some warning");
\& warnings::warnif($object, "some warning");
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
The \f(CW\*(C`warnings\*(C'\fR pragma gives control over which warnings are enabled in
which parts of a Perl program. It's a more flexible alternative for
both the command line flag \fB\-w\fR and the equivalent Perl variable,
\&\f(CW$^W\fR.
.PP
This pragma works just like the \f(CW\*(C`strict\*(C'\fR 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 \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR). This allows
authors to independently define the degree of warning checks that will
be applied to their module.
.PP
By default, optional warnings are disabled, so any legacy code that
doesn't attempt to control the warnings will work unchanged.
.PP
All warnings are enabled in a block by either of these:
.PP
.Vb 2
\& use warnings;
\& use warnings \*(Aqall\*(Aq;
.Ve
.PP
Similarly all warnings are disabled in a block by either of these:
.PP
.Vb 2
\& no warnings;
\& no warnings \*(Aqall\*(Aq;
.Ve
.PP
For example, consider the code below:
.PP
.Vb 7
\& use warnings;
\& my @x;
\& {
\& no warnings;
\& my $y = @x[0];
\& }
\& my $z = @x[0];
.Ve
.PP
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 \f(CW$z\fR will trip the \f(CW"Scalar value @x[0] better written as $x[0]"\fR
warning, but the assignment to the scalar \f(CW$y\fR will not.
.PP
All warnings are enabled automatically within the scope of
a \f(CW\*(C`use v5.35\*(C'\fR (or higher) declaration.
.SS "Default Warnings and Optional Warnings"
.IX Subsection "Default Warnings and Optional Warnings"
Before the introduction of lexical warnings, Perl had two classes of
warnings: mandatory and optional.
.PP
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 \f(CW"isn\*(Aqt numeric"\fR
warning about the "2:".
.PP
.Vb 1
\& my $x = "2:" + 3;
.Ve
.PP
With the introduction of lexical warnings, mandatory warnings now become
\&\fIdefault\fR 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 \f(CW"isn\*(Aqt numeric"\fR warning will only
be reported for the \f(CW$x\fR variable.
.PP
.Vb 3
\& my $x = "2:" + 3;
\& no warnings;
\& my $y = "2:" + 3;
.Ve
.PP
Note that neither the \fB\-w\fR flag or the \f(CW$^W\fR can be used to
disable/enable default warnings. They are still mandatory in this case.
.SS """Negative warnings"""
.IX Subsection """Negative warnings"""
As a convenience, you can (as of Perl 5.34) pass arguments to the
\&\f(CWimport()\fR method both positively and negatively. Negative warnings
are those with a \f(CW\*(C`\-\*(C'\fR sign prepended to their names; positive warnings
are anything else. This lets you turn on some warnings and turn off
others in one command. So, assuming that you've already turned on a
bunch of warnings but want to tweak them a bit in some block, you can
do this:
.PP
.Vb 4
\& {
\& use warnings qw(uninitialized \-redefine);
\& ...
\& }
.Ve
.PP
which is equivalent to:
.PP
.Vb 5
\& {
\& use warnings qw(uninitialized);
\& no warnings qw(redefine);
\& ...
\& }
.Ve
.PP
The argument list is processed in the order you specify. So, for example, if you
don't want to be warned about use of experimental features, except for \f(CW\*(C`somefeature\*(C'\fR
that you really dislike, you can say this:
.PP
.Vb 1
\& use warnings qw(all \-experimental experimental::somefeature);
.Ve
.PP
which is equivalent to:
.PP
.Vb 3
\& use warnings \*(Aqall\*(Aq;
\& no warnings \*(Aqexperimental\*(Aq;
\& use warnings \*(Aqexperimental::somefeature\*(Aq;
.Ve
.PP
As experimental features become regular features of Perl,
the corresponding warnings are not printed anymore.
They also stop being listed in the "Category Hierarchy" below.
.PP
It is still possible to request turning on or off these warnings,
but doing so has no effect.
.ie n .SS "What's wrong with \fB\-w\fP and $^W"
.el .SS "What's wrong with \fB\-w\fP and \f(CW$^W\fP"
.IX Subsection "What's wrong with -w and $^W"
Although very useful, the big problem with using \fB\-w\fR 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 \fB\-w\fR flag in this case, you
end up enabling warnings in pieces of code that you haven't written.
.PP
Similarly, using \f(CW$^W\fR 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:
.PP
.Vb 5
\& {
\& local ($^W) = 0;
\& my $x =+ 2;
\& my $y; chop $y;
\& }
.Ve
.PP
When this code is run with the \fB\-w\fR flag, a warning will be produced
for the \f(CW$x\fR line: \f(CW"Reversed += operator"\fR.
.PP
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:
.PP
.Vb 5
\& {
\& BEGIN { $^W = 0 }
\& my $x =+ 2;
\& my $y; chop $y;
\& }
.Ve
.PP
And note that unlike the first example, this will permanently set \f(CW$^W\fR
since it cannot both run during compile-time and be localized to a
run-time block.
.PP
The other big problem with \f(CW$^W\fR 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 \fB\-w\fR flag), the second call
to \f(CW\*(C`doit\*(C'\fR will trip a \f(CW"Use of uninitialized value"\fR warning, whereas
the first will not.
.PP
.Vb 4
\& sub doit
\& {
\& my $y; chop $y;
\& }
\&
\& doit();
\&
\& {
\& local ($^W) = 1;
\& doit()
\& }
.Ve
.PP
This is a side-effect of \f(CW$^W\fR being dynamically scoped.
.PP
Lexical warnings get around these limitations by allowing finer control
over where warnings can or can't be tripped.
.SS "Controlling Warnings from the Command Line"
.IX Subsection "Controlling Warnings from the Command Line"
There are three Command Line flags that can be used to control when
warnings are (or aren't) produced:
.IP \fB\-w\fR 5
.IX Xref "-w"
.IX Item "-w"
This is the existing flag. If the lexical warnings pragma is \fBnot\fR
used in any of your code, or any of the modules that you use, this flag
will enable warnings everywhere. See "Backward Compatibility" for
details of how this flag interacts with lexical warnings.
.IP \fB\-W\fR 5
.IX Xref "-W"
.IX Item "-W"
If the \fB\-W\fR flag is used on the command line, it will enable all warnings
throughout the program regardless of whether warnings were disabled
locally using \f(CW\*(C`no warnings\*(C'\fR or \f(CW\*(C`$^W =0\*(C'\fR.
This includes all files that get
included via \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR.
Think of it as the Perl equivalent of the "lint" command.
.IP \fB\-X\fR 5
.IX Xref "-X"
.IX Item "-X"
Does the exact opposite to the \fB\-W\fR flag, i.e. it disables all warnings.
.SS "Backward Compatibility"
.IX Subsection "Backward Compatibility"
If you are used to working with a version of Perl prior to the
introduction of lexically scoped warnings, or have code that uses both
lexical warnings and \f(CW$^W\fR, this section will describe how they interact.
.PP
How Lexical Warnings interact with \fB\-w\fR/\f(CW$^W\fR:
.IP 1. 5
If none of the three command line flags (\fB\-w\fR, \fB\-W\fR or \fB\-X\fR) that
control warnings is used and neither \f(CW$^W\fR nor the \f(CW\*(C`warnings\*(C'\fR pragma
are used, then default warnings will be enabled and optional warnings
disabled.
This means that legacy code that doesn't attempt to control the warnings
will work unchanged.
.IP 2. 5
The \fB\-w\fR flag just sets the global \f(CW$^W\fR variable as in 5.005. This
means that any legacy code that currently relies on manipulating \f(CW$^W\fR
to control warning behavior will still work as is.
.IP 3. 5
Apart from now being a boolean, the \f(CW$^W\fR variable operates in exactly
the same horrible uncontrolled global way, except that it cannot
disable/enable default warnings.
.IP 4. 5
If a piece of code is under the control of the \f(CW\*(C`warnings\*(C'\fR pragma,
both the \f(CW$^W\fR variable and the \fB\-w\fR flag will be ignored for the
scope of the lexical warning.
.IP 5. 5
The only way to override a lexical warnings setting is with the \fB\-W\fR
or \fB\-X\fR command line flags.
.PP
The combined effect of 3 & 4 is that it will allow code which uses
the \f(CW\*(C`warnings\*(C'\fR pragma to control the warning behavior of $^W\-type
code (using a \f(CW\*(C`local $^W=0\*(C'\fR) if it really wants to, but not vice-versa.
.SS "Category Hierarchy"
.IX Xref "warning, categories"
.IX Subsection "Category Hierarchy"
A hierarchy of "categories" have been defined to allow groups of warnings
to be enabled/disabled in isolation.
.PP
The current hierarchy is:
.PP
.Vb 10
\& all \-+
\& |
\& +\- closure
\& |
\& +\- deprecated \-\-\-\-+
\& | |
\& | +\- deprecated::apostrophe_as_package_separator
\& | |
\& | +\- deprecated::delimiter_will_be_paired
\& | |
\& | +\- deprecated::dot_in_inc
\& | |
\& | +\- deprecated::goto_construct
\& | |
\& | +\- deprecated::smartmatch
\& | |
\& | +\- deprecated::unicode_property_name
\& | |
\& | +\- deprecated::version_downgrade
\& |
\& +\- exiting
\& |
\& +\- experimental \-\-+
\& | |
\& | +\- experimental::args_array_with_signatures
\& | |
\& | +\- experimental::builtin
\& | |
\& | +\- experimental::class
\& | |
\& | +\- experimental::const_attr
\& | |
\& | +\- experimental::declared_refs
\& | |
\& | +\- experimental::defer
\& | |
\& | +\- experimental::extra_paired_delimiters
\& | |
\& | +\- experimental::for_list
\& | |
\& | +\- experimental::private_use
\& | |
\& | +\- experimental::re_strict
\& | |
\& | +\- experimental::refaliasing
\& | |
\& | +\- experimental::regex_sets
\& | |
\& | +\- experimental::try
\& | |
\& | +\- experimental::uniprop_wildcards
\& | |
\& | +\- experimental::vlb
\& |
\& +\- glob
\& |
\& +\- imprecision
\& |
\& +\- io \-\-\-\-\-\-\-\-\-\-\-\-+
\& | |
\& | +\- closed
\& | |
\& | +\- exec
\& | |
\& | +\- layer
\& | |
\& | +\- newline
\& | |
\& | +\- pipe
\& | |
\& | +\- syscalls
\& | |
\& | +\- unopened
\& |
\& +\- locale
\& |
\& +\- misc
\& |
\& +\- missing
\& |
\& +\- numeric
\& |
\& +\- once
\& |
\& +\- overflow
\& |
\& +\- pack
\& |
\& +\- portable
\& |
\& +\- recursion
\& |
\& +\- redefine
\& |
\& +\- redundant
\& |
\& +\- regexp
\& |
\& +\- scalar
\& |
\& +\- severe \-\-\-\-\-\-\-\-+
\& | |
\& | +\- debugging
\& | |
\& | +\- inplace
\& | |
\& | +\- internal
\& | |
\& | +\- malloc
\& |
\& +\- shadow
\& |
\& +\- signal
\& |
\& +\- substr
\& |
\& +\- syntax \-\-\-\-\-\-\-\-+
\& | |
\& | +\- ambiguous
\& | |
\& | +\- bareword
\& | |
\& | +\- digit
\& | |
\& | +\- illegalproto
\& | |
\& | +\- parenthesis
\& | |
\& | +\- precedence
\& | |
\& | +\- printf
\& | |
\& | +\- prototype
\& | |
\& | +\- qw
\& | |
\& | +\- reserved
\& | |
\& | +\- semicolon
\& |
\& +\- taint
\& |
\& +\- threads
\& |
\& +\- uninitialized
\& |
\& +\- unpack
\& |
\& +\- untie
\& |
\& +\- utf8 \-\-\-\-\-\-\-\-\-\-+
\& | |
\& | +\- non_unicode
\& | |
\& | +\- nonchar
\& | |
\& | +\- surrogate
\& |
\& +\- void
.Ve
.PP
Just like the "strict" pragma any of these categories can be combined
.PP
.Vb 2
\& use warnings qw(void redefine);
\& no warnings qw(io syntax untie);
.Ve
.PP
Also like the "strict" pragma, if there is more than one instance of the
\&\f(CW\*(C`warnings\*(C'\fR pragma in a given scope the cumulative effect is additive.
.PP
.Vb 5
\& use warnings qw(void); # only "void" warnings enabled
\& ...
\& use warnings qw(io); # only "void" & "io" warnings enabled
\& ...
\& no warnings qw(void); # only "io" warnings enabled
.Ve
.PP
To determine which category a specific warning has been assigned to see
perldiag.
.PP
Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a
sub-category of the "syntax" category. It is now a top-level category
in its own right.
.PP
Note: Before 5.21.0, the "missing" lexical warnings category was
internally defined to be the same as the "uninitialized" category. It
is now a top-level category in its own right.
.SS "Fatal Warnings"
.IX Xref "warning, fatal"
.IX Subsection "Fatal Warnings"
The presence of the word "FATAL" in the category list will escalate
warnings in those categories into fatal errors in that lexical scope.
.PP
\&\fBNOTE:\fR FATAL warnings should be used with care, particularly
\&\f(CW\*(C`FATAL => \*(Aqall\*(Aq\*(C'\fR.
.PP
Libraries using warnings::warn for custom warning categories
generally don't expect warnings::warn to be fatal and can wind up
in an unexpected state as a result. For XS modules issuing categorized
warnings, such unanticipated exceptions could also expose memory leak bugs.
.PP
Moreover, the Perl interpreter itself has had serious bugs involving
fatalized warnings. For a summary of resolved and unresolved problems as
of January 2015, please see
this perl5\-porters post <http://www.nntp.perl.org/group/perl.perl5.porters/2015/01/msg225235.html>.
.PP
While some developers find fatalizing some warnings to be a useful
defensive programming technique, using \f(CW\*(C`FATAL => \*(Aqall\*(Aq\*(C'\fR to fatalize
all possible warning categories \-\- including custom ones \-\- is particularly
risky. Therefore, the use of \f(CW\*(C`FATAL => \*(Aqall\*(Aq\*(C'\fR is
discouraged.
.PP
The strictures module on CPAN offers one example of
a warnings subset that the module's authors believe is relatively safe to
fatalize.
.PP
\&\fBNOTE:\fR Users of FATAL warnings, especially those using
\&\f(CW\*(C`FATAL => \*(Aqall\*(Aq\*(C'\fR, should be fully aware that they are risking future
portability of their programs by doing so. Perl makes absolutely no
commitments to not introduce new warnings or warnings categories in the
future; indeed, we explicitly reserve the right to do so. Code that may
not warn now may warn in a future release of Perl if the Perl5 development
team deems it in the best interests of the community to do so. Should code
using FATAL warnings break due to the introduction of a new warning we will
NOT consider it an incompatible change. Users of FATAL warnings should
take special caution during upgrades to check to see if their code triggers
any new warnings and should pay particular attention to the fine print of
the documentation of the features they use to ensure they do not exploit
features that are documented as risky, deprecated, or unspecified, or where
the documentation says "so don't do that", or anything with the same sense
and spirit. Use of such features in combination with FATAL warnings is
ENTIRELY AT THE USER'S RISK.
.PP
The following documentation describes how to use FATAL warnings but the
perl5 porters strongly recommend that you understand the risks before doing
so, especially for library code intended for use by others, as there is no
way for downstream users to change the choice of fatal categories.
.PP
In the code below, the use of \f(CW\*(C`time\*(C'\fR, \f(CW\*(C`length\*(C'\fR
and \f(CW\*(C`join\*(C'\fR can all produce a \f(CW"Useless use of xxx in void context"\fR
warning.
.PP
.Vb 1
\& use warnings;
\&
\& time;
\&
\& {
\& use warnings FATAL => qw(void);
\& length "abc";
\& }
\&
\& join "", 1,2,3;
\&
\& print "done\en";
.Ve
.PP
When run it produces this output
.PP
.Vb 2
\& Useless use of time in void context at fatal line 3.
\& Useless use of length in void context at fatal line 7.
.Ve
.PP
The scope where \f(CW\*(C`length\*(C'\fR is used has escalated the \f(CW\*(C`void\*(C'\fR warnings
category into a fatal error, so the program terminates immediately when it
encounters the warning.
.PP
To explicitly turn off a "FATAL" warning you just disable the warning
it is associated with. So, for example, to disable the "void" warning
in the example above, either of these will do the trick:
.PP
.Vb 2
\& no warnings qw(void);
\& no warnings FATAL => qw(void);
.Ve
.PP
If you want to downgrade a warning that has been escalated into a fatal
error back to a normal warning, you can use the "NONFATAL" keyword. For
example, the code below will promote all warnings into fatal errors,
except for those in the "syntax" category.
.PP
.Vb 1
\& use warnings FATAL => \*(Aqall\*(Aq, NONFATAL => \*(Aqsyntax\*(Aq;
.Ve
.PP
As of Perl 5.20, instead of \f(CW\*(C`use warnings FATAL => \*(Aqall\*(Aq;\*(C'\fR you can
use:
.PP
.Vb 2
\& use v5.20; # Perl 5.20 or greater is required for the following
\& use warnings \*(AqFATAL\*(Aq; # short form of "use warnings FATAL => \*(Aqall\*(Aq;"
.Ve
.PP
However, you should still heed the guidance earlier in this section against
using \f(CW\*(C`use warnings FATAL => \*(Aqall\*(Aq;\*(C'\fR.
.PP
If you want your program to be compatible with versions of Perl before
5.20, you must use \f(CW\*(C`use warnings FATAL => \*(Aqall\*(Aq;\*(C'\fR instead. (In
previous versions of Perl, the behavior of the statements
\&\f(CW\*(C`use warnings \*(AqFATAL\*(Aq;\*(C'\fR, \f(CW\*(C`use warnings \*(AqNONFATAL\*(Aq;\*(C'\fR and
\&\f(CW\*(C`no warnings \*(AqFATAL\*(Aq;\*(C'\fR was unspecified; they did not behave as if
they included the \f(CW\*(C`=> \*(Aqall\*(Aq\*(C'\fR portion. As of 5.20, they do.)
.SS "Reporting Warnings from a Module"
.IX Xref "warning, reporting warning, registering"
.IX Subsection "Reporting Warnings from a Module"
The \f(CW\*(C`warnings\*(C'\fR pragma provides a number of functions that are useful for
module authors. These are used when you want to report a module-specific
warning to a calling module has enabled warnings via the \f(CW\*(C`warnings\*(C'\fR
pragma.
.PP
Consider the module \f(CW\*(C`MyMod::Abc\*(C'\fR below.
.PP
.Vb 1
\& package MyMod::Abc;
\&
\& use warnings::register;
\&
\& sub open {
\& my $path = shift;
\& if ($path !~ m#^/#) {
\& warnings::warn("changing relative path to /var/abc")
\& if warnings::enabled();
\& $path = "/var/abc/$path";
\& }
\& }
\&
\& 1;
.Ve
.PP
The call to \f(CW\*(C`warnings::register\*(C'\fR will create a new warnings category
called "MyMod::Abc", i.e. the new category name matches the current
package name. The \f(CW\*(C`open\*(C'\fR function in the module will display a warning
message if it gets given a relative path as a parameter. This warnings
will only be displayed if the code that uses \f(CW\*(C`MyMod::Abc\*(C'\fR has actually
enabled them with the \f(CW\*(C`warnings\*(C'\fR pragma like below.
.PP
.Vb 4
\& use MyMod::Abc;
\& use warnings \*(AqMyMod::Abc\*(Aq;
\& ...
\& abc::open("../fred.txt");
.Ve
.PP
It is also possible to test whether the pre-defined warnings categories are
set in the calling module with the \f(CW\*(C`warnings::enabled\*(C'\fR function. Consider
this snippet of code:
.PP
.Vb 1
\& package MyMod::Abc;
\&
\& sub open {
\& if (warnings::enabled("deprecated")) {
\& warnings::warn("deprecated",
\& "open is deprecated, use new instead");
\& }
\& new(@_);
\& }
\&
\& sub new
\& ...
\& 1;
.Ve
.PP
The function \f(CW\*(C`open\*(C'\fR has been deprecated, so code has been included to
display a warning message whenever the calling module has (at least) the
"deprecated" warnings category enabled. Something like this, say.
.PP
.Vb 4
\& use warnings \*(Aqdeprecated\*(Aq;
\& use MyMod::Abc;
\& ...
\& MyMod::Abc::open($filename);
.Ve
.PP
Either the \f(CW\*(C`warnings::warn\*(C'\fR or \f(CW\*(C`warnings::warnif\*(C'\fR function should be
used to actually display the warnings message. This is because they can
make use of the feature that allows warnings to be escalated into fatal
errors. So in this case
.PP
.Vb 4
\& use MyMod::Abc;
\& use warnings FATAL => \*(AqMyMod::Abc\*(Aq;
\& ...
\& MyMod::Abc::open(\*(Aq../fred.txt\*(Aq);
.Ve
.PP
the \f(CW\*(C`warnings::warnif\*(C'\fR function will detect this and die after
displaying the warning message.
.PP
The three warnings functions, \f(CW\*(C`warnings::warn\*(C'\fR, \f(CW\*(C`warnings::warnif\*(C'\fR
and \f(CW\*(C`warnings::enabled\*(C'\fR can optionally take an object reference in place
of a category name. In this case the functions will use the class name
of the object as the warnings category.
.PP
Consider this example:
.PP
.Vb 1
\& package Original;
\&
\& no warnings;
\& use warnings::register;
\&
\& sub new
\& {
\& my $class = shift;
\& bless [], $class;
\& }
\&
\& sub check
\& {
\& my $self = shift;
\& my $value = shift;
\&
\& if ($value % 2 && warnings::enabled($self))
\& { warnings::warn($self, "Odd numbers are unsafe") }
\& }
\&
\& sub doit
\& {
\& my $self = shift;
\& my $value = shift;
\& $self\->check($value);
\& # ...
\& }
\&
\& 1;
\&
\& package Derived;
\&
\& use warnings::register;
\& use Original;
\& our @ISA = qw( Original );
\& sub new
\& {
\& my $class = shift;
\& bless [], $class;
\& }
\&
\&
\& 1;
.Ve
.PP
The code below makes use of both modules, but it only enables warnings from
\&\f(CW\*(C`Derived\*(C'\fR.
.PP
.Vb 7
\& use Original;
\& use Derived;
\& use warnings \*(AqDerived\*(Aq;
\& my $x = Original\->new();
\& $x\->doit(1);
\& my $y = Derived\->new();
\& $x\->doit(1);
.Ve
.PP
When this code is run only the \f(CW\*(C`Derived\*(C'\fR object, \f(CW$y\fR, will generate
a warning.
.PP
.Vb 1
\& Odd numbers are unsafe at main.pl line 7
.Ve
.PP
Notice also that the warning is reported at the line where the object is first
used.
.PP
When registering new categories of warning, you can supply more names to
warnings::register like this:
.PP
.Vb 2
\& package MyModule;
\& use warnings::register qw(format precision);
\&
\& ...
\&
\& warnings::warnif(\*(AqMyModule::format\*(Aq, \*(Aq...\*(Aq);
.Ve
.SH FUNCTIONS
.IX Header "FUNCTIONS"
Note: The functions with names ending in \f(CW\*(C`_at_level\*(C'\fR were added in Perl
5.28.
.IP "use warnings::register" 4
.IX Item "use warnings::register"
Creates a new warnings category with the same name as the package where
the call to the pragma is used.
.IP \fBwarnings::enabled()\fR 4
.IX Item "warnings::enabled()"
Use the warnings category with the same name as the current package.
.Sp
Return TRUE if that warnings category is enabled in the calling module.
Otherwise returns FALSE.
.IP warnings::enabled($category) 4
.IX Item "warnings::enabled($category)"
Return TRUE if the warnings category, \f(CW$category\fR, is enabled in the
calling module.
Otherwise returns FALSE.
.IP warnings::enabled($object) 4
.IX Item "warnings::enabled($object)"
Use the name of the class for the object reference, \f(CW$object\fR, as the
warnings category.
.Sp
Return TRUE if that warnings category is enabled in the first scope
where the object is used.
Otherwise returns FALSE.
.ie n .IP "warnings::enabled_at_level($category, $level)" 4
.el .IP "warnings::enabled_at_level($category, \f(CW$level\fR)" 4
.IX Item "warnings::enabled_at_level($category, $level)"
Like \f(CW\*(C`warnings::enabled\*(C'\fR, but \f(CW$level\fR specifies the exact call frame, 0
being the immediate caller.
.IP \fBwarnings::fatal_enabled()\fR 4
.IX Item "warnings::fatal_enabled()"
Return TRUE if the warnings category with the same name as the current
package has been set to FATAL in the calling module.
Otherwise returns FALSE.
.IP warnings::fatal_enabled($category) 4
.IX Item "warnings::fatal_enabled($category)"
Return TRUE if the warnings category \f(CW$category\fR has been set to FATAL in
the calling module.
Otherwise returns FALSE.
.IP warnings::fatal_enabled($object) 4
.IX Item "warnings::fatal_enabled($object)"
Use the name of the class for the object reference, \f(CW$object\fR, as the
warnings category.
.Sp
Return TRUE if that warnings category has been set to FATAL in the first
scope where the object is used.
Otherwise returns FALSE.
.ie n .IP "warnings::fatal_enabled_at_level($category, $level)" 4
.el .IP "warnings::fatal_enabled_at_level($category, \f(CW$level\fR)" 4
.IX Item "warnings::fatal_enabled_at_level($category, $level)"
Like \f(CW\*(C`warnings::fatal_enabled\*(C'\fR, but \f(CW$level\fR specifies the exact call frame,
0 being the immediate caller.
.IP warnings::warn($message) 4
.IX Item "warnings::warn($message)"
Print \f(CW$message\fR to STDERR.
.Sp
Use the warnings category with the same name as the current package.
.Sp
If that warnings category has been set to "FATAL" in the calling module
then die. Otherwise return.
.ie n .IP "warnings::warn($category, $message)" 4
.el .IP "warnings::warn($category, \f(CW$message\fR)" 4
.IX Item "warnings::warn($category, $message)"
Print \f(CW$message\fR to STDERR.
.Sp
If the warnings category, \f(CW$category\fR, has been set to "FATAL" in the
calling module then die. Otherwise return.
.ie n .IP "warnings::warn($object, $message)" 4
.el .IP "warnings::warn($object, \f(CW$message\fR)" 4
.IX Item "warnings::warn($object, $message)"
Print \f(CW$message\fR to STDERR.
.Sp
Use the name of the class for the object reference, \f(CW$object\fR, as the
warnings category.
.Sp
If that warnings category has been set to "FATAL" in the scope where \f(CW$object\fR
is first used then die. Otherwise return.
.ie n .IP "warnings::warn_at_level($category, $level, $message)" 4
.el .IP "warnings::warn_at_level($category, \f(CW$level\fR, \f(CW$message\fR)" 4
.IX Item "warnings::warn_at_level($category, $level, $message)"
Like \f(CW\*(C`warnings::warn\*(C'\fR, but \f(CW$level\fR specifies the exact call frame,
0 being the immediate caller.
.IP warnings::warnif($message) 4
.IX Item "warnings::warnif($message)"
Equivalent to:
.Sp
.Vb 2
\& if (warnings::enabled())
\& { warnings::warn($message) }
.Ve
.ie n .IP "warnings::warnif($category, $message)" 4
.el .IP "warnings::warnif($category, \f(CW$message\fR)" 4
.IX Item "warnings::warnif($category, $message)"
Equivalent to:
.Sp
.Vb 2
\& if (warnings::enabled($category))
\& { warnings::warn($category, $message) }
.Ve
.ie n .IP "warnings::warnif($object, $message)" 4
.el .IP "warnings::warnif($object, \f(CW$message\fR)" 4
.IX Item "warnings::warnif($object, $message)"
Equivalent to:
.Sp
.Vb 2
\& if (warnings::enabled($object))
\& { warnings::warn($object, $message) }
.Ve
.ie n .IP "warnings::warnif_at_level($category, $level, $message)" 4
.el .IP "warnings::warnif_at_level($category, \f(CW$level\fR, \f(CW$message\fR)" 4
.IX Item "warnings::warnif_at_level($category, $level, $message)"
Like \f(CW\*(C`warnings::warnif\*(C'\fR, but \f(CW$level\fR specifies the exact call frame,
0 being the immediate caller.
.IP warnings::register_categories(@names) 4
.IX Item "warnings::register_categories(@names)"
This registers warning categories for the given names and is primarily for
use by the warnings::register pragma.
.PP
See also "Pragmatic Modules" in perlmodlib and perldiag.
|