File: warnings.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (862 lines) | stat: -rw-r--r-- 66,340 bytes parent folder | download
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>warnings - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>warnings</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-pragmas.html">Pragmas</a> &gt;
      
    
    warnings
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>warnings</h1>


  <!--    -->
<ul><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Default-Warnings-and-Optional-Warnings">Default Warnings and Optional Warnings</a><li><a href="#What's-wrong-with-*-w*-and-%24%5eW">What's wrong with *-w* and $^W</a><li><a href="#Controlling-Warnings-from-the-Command-Line">Controlling Warnings from the Command Line</a><li><a href="#Backward-Compatibility">Backward Compatibility</a><li><a href="#Category-Hierarchy">Category Hierarchy
</a><li><a href="#Fatal-Warnings">Fatal Warnings
</a><li><a href="#Reporting-Warnings-from-a-Module">Reporting Warnings from a Module
 </a></ul><li><a href="#FUNCTIONS">FUNCTIONS</a></ul><a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&quot;all&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&quot;all&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></li><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">warnings::warn</span><span class="s">(</span><span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="q">&quot;void&quot;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">warnings::warn</span><span class="s">(</span><span class="q">&quot;void&quot;</span><span class="cm">,</span> <span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="i">$object</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">warnings::warn</span><span class="s">(</span><span class="i">$object</span><span class="cm">,</span> <span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">warnings::warnif</span><span class="s">(</span><span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">warnings::warnif</span><span class="s">(</span><span class="q">&quot;void&quot;</span><span class="cm">,</span> <span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">warnings::warnif</span><span class="s">(</span><span class="i">$object</span><span class="cm">,</span> <span class="q">&quot;some warning&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>The <code class="inline"><span class="w">warnings</span></code>
 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 <b>-w</b> and the equivalent Perl variable,
<code class="inline"><span class="i">$^W</span></code>
.</p>
<p>This pragma works just like the <code class="inline"><span class="w">strict</span></code>
 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 <code class="inline"><a class="l_k" href="functions/use.html">use</a></code>, <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>).  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 class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;all&#39;</span><span class="sc">;</span></li></ol></pre><p>Similarly all warnings are disabled in a block by either of these:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&#39;all&#39;</span><span class="sc">;</span></li></ol></pre><p>For example, consider the code below:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@a</span><span class="sc">;</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = <span class="i">@a</span>[<span class="n">0</span>]<span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <span class="i">@a</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></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 class="inline"><span class="i">$c</span></code>
 will trip the <code class="inline"><span class="q">&quot;Scalar value @a[0] better written as $a[0]&quot;</span></code>

warning, but the assignment to the scalar <code class="inline"><span class="i">$b</span></code>
 will not.</p>
<a name="Default-Warnings-and-Optional-Warnings"></a><h2>Default Warnings and Optional Warnings</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 class="inline"><span class="q">&quot;isn&#39;t numeric&quot;</span></code>

warning about the "2:".</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="q">&quot;2:&quot;</span> + <span class="n">3</span><span class="sc">;</span></li></ol></pre><p>With the introduction of lexical warnings, mandatory warnings now become
<i>default</i> 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 class="inline"><span class="q">&quot;isn&#39;t numeric&quot;</span></code>
 warning will only
be reported for the <code class="inline"><span class="i">$a</span></code>
 variable.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="q">&quot;2:&quot;</span> + <span class="n">3</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = <span class="q">&quot;2:&quot;</span> + <span class="n">3</span><span class="sc">;</span></li></ol></pre><p>Note that neither the <b>-w</b> flag or the <code class="inline"><span class="i">$^W</span></code>
 can be used to
disable/enable default warnings.  They are still mandatory in this case.</p>
<a name="What's-wrong-with-*-w*-and-%24%5eW"></a><h2>What's wrong with <b>-w</b> and <code class="inline"><span class="i">$^W</span></code>
</h2>
<p>Although very useful, the big problem with using <b>-w</b> 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 <b>-w</b> flag in this case, you
end up enabling warnings in pieces of code that you haven't written.</p>
<p>Similarly, using <code class="inline"><span class="i">$^W</span></code>
 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 class="verbatim"><ol><li>     <span class="s">{</span></li><li>         <a class="l_k" href="functions/local.html">local</a> <span class="s">(</span><span class="i">$^W</span><span class="s">)</span> = <span class="n">0</span><span class="sc">;</span></li><li>	 <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> =+ <span class="n">2</span><span class="sc">;</span></li><li>	 <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span><span class="sc">;</span> <a class="l_k" href="functions/chop.html">chop</a> <span class="i">$b</span><span class="sc">;</span></li><li>     <span class="s">}</span></li></ol></pre><p>When this code is run with the <b>-w</b> flag, a warning will be produced
for the <code class="inline"><span class="i">$a</span></code>
 line:  <code class="inline"><span class="q">&quot;Reversed += operator&quot;</span></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 class="verbatim"><ol><li>     <span class="s">{</span></li><li>         BEGIN <span class="s">{</span> <span class="i">$^W</span> = <span class="n">0</span> <span class="s">}</span></li><li>	 <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> =+ <span class="n">2</span><span class="sc">;</span></li><li>	 <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span><span class="sc">;</span> <a class="l_k" href="functions/chop.html">chop</a> <span class="i">$b</span><span class="sc">;</span></li><li>     <span class="s">}</span></li></ol></pre><p>The other big problem with <code class="inline"><span class="i">$^W</span></code>
 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 <b>-w</b> flag), the second call
to <code class="inline"><span class="w">doit</span></code>
 will trip a <code class="inline"><span class="q">&quot;Use of uninitialized value&quot;</span></code>
 warning, whereas
the first will not.</p>
<pre class="verbatim"><ol><li><a name="doit"></a>    sub <span class="m">doit</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span><span class="sc">;</span> <a class="l_k" href="functions/chop.html">chop</a> <span class="i">$b</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">doit</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/local.html">local</a> <span class="s">(</span><span class="i">$^W</span><span class="s">)</span> = <span class="n">1</span><span class="sc">;</span></li><li>        <span class="i">doit</span><span class="s">(</span><span class="s">)</span></li><li>    <span class="s">}</span></li></ol></pre><p>This is a side-effect of <code class="inline"><span class="i">$^W</span></code>
 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>
<a name="Controlling-Warnings-from-the-Command-Line"></a><h2>Controlling Warnings from the Command Line</h2>
<p>There are three Command Line flags that can be used to control when
warnings are (or aren't) produced:</p>
<ul>
<li><a name="*-w*"></a><b><b>-w</b>
</b>
<p>This is  the existing flag.  If the lexical warnings pragma is <b>not</b>
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>
</li>
<li><a name="*-W*"></a><b><b>-W</b>
</b>
<p>If the <b>-W</b> flag is used on the command line, it will enable all warnings
throughout the program regardless of whether warnings were disabled
locally using <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span></code>
 or <code class="inline"><span class="i">$^W</span> =<span class="n">0</span></code>
.
This includes all files that get
included via <code class="inline"><a class="l_k" href="functions/use.html">use</a></code>, <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>.
Think of it as the Perl equivalent of the "lint" command.</p>
</li>
<li><a name="*-X*"></a><b><b>-X</b>
</b>
<p>Does the exact opposite to the <b>-W</b> flag, i.e. it disables all warnings.</p>
</li>
</ul>
<a name="Backward-Compatibility"></a><h2>Backward Compatibility</h2>
<p>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 <code class="inline"><span class="i">$^W</span></code>
, this section will describe how they interact.</p>
<p>How Lexical Warnings interact with <b>-w</b>/<code class="inline"><span class="i">$^W</span></code>
:</p>
<dl>
<dt>1.</dt><dd>
<p>If none of the three command line flags (<b>-w</b>, <b>-W</b> or <b>-X</b>) that
control warnings is used and neither <code class="inline"><span class="i">$^W</span></code>
 nor the <code class="inline"><span class="w">warnings</span></code>
 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.</p>
</dd>
<dt>2.</dt><dd>
<p>The <b>-w</b> flag just sets the global <code class="inline"><span class="i">$^W</span></code>
 variable as in 5.005.  This
means that any legacy code that currently relies on manipulating <code class="inline"><span class="i">$^W</span></code>

to control warning behavior will still work as is.</p>
</dd>
<dt>3.</dt><dd>
<p>Apart from now being a boolean, the <code class="inline"><span class="i">$^W</span></code>
 variable operates in exactly
the same horrible uncontrolled global way, except that it cannot
disable/enable default warnings.</p>
</dd>
<dt>4.</dt><dd>
<p>If a piece of code is under the control of the <code class="inline"><span class="w">warnings</span></code>
 pragma,
both the <code class="inline"><span class="i">$^W</span></code>
 variable and the <b>-w</b> flag will be ignored for the
scope of the lexical warning.</p>
</dd>
<dt>5.</dt><dd>
<p>The only way to override a lexical warnings setting is with the <b>-W</b>
or <b>-X</b> command line flags.</p>
</dd>
</dl>
<p>The combined effect of 3 &amp; 4 is that it will allow code which uses
the <code class="inline"><span class="w">warnings</span></code>
 pragma to control the warning behavior of $^W-type
code (using a <code class="inline"><a class="l_k" href="functions/local.html">local</a> <span class="i">$^W</span>=<span class="n">0</span></code>
) if it really wants to, but not vice-versa.</p>
<a name="Category-Hierarchy"></a><h2>Category Hierarchy
</h2>
<p>A hierarchy of "categories" have been defined to allow groups of warnings
to be enabled/disabled in isolation.</p>
<p>The current hierarchy is:</p>
<pre class="verbatim"><ol><li>    <span class="w">all</span> -+</li><li>         |</li><li>         +- <span class="w">closure</span></li><li>         |</li><li>         +- <span class="w">deprecated</span></li><li>         |</li><li>         +- <span class="w">exiting</span></li><li>         |</li><li>         +- <span class="w">experimental</span> --+</li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::bitwise</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::const_attr</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::declared_refs</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::lexical_subs</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::postderef</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::re_strict</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::refaliasing</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::regex_sets</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::signatures</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::smartmatch</span></li><li>         |                 |</li><li>         |                 +- <span class="w">experimental::win32_perlio</span></li><li>         |</li><li>         +- <a class="l_k" href="functions/glob.html">glob</a></li><li>         |</li><li>         +- <span class="w">imprecision</span></li><li>         |</li><li>         +- <span class="w">io</span> ------------+</li><li>         |                 |</li><li>         |                 +- <span class="w">closed</span></li><li>         |                 |</li><li>         |                 +- <a class="l_k" href="functions/exec.html">exec</a></li><li>         |                 |</li><li>         |                 +- <span class="w">layer</span></li><li>         |                 |</li><li>         |                 +- <span class="w">newline</span></li><li>         |                 |</li><li>         |                 +- <a class="l_k" href="functions/pipe.html">pipe</a></li><li>         |                 |</li><li>         |                 +- <span class="w">syscalls</span></li><li>         |                 |</li><li>         |                 +- <span class="w">unopened</span></li><li>         |</li><li>         +- <span class="w">locale</span></li><li>         |</li><li>         +- <span class="w">misc</span></li><li>         |</li><li>         +- <span class="w">missing</span></li><li>         |</li><li>         +- <span class="w">numeric</span></li><li>         |</li><li>         +- <span class="w">once</span></li><li>         |</li><li>         +- <span class="w">overflow</span></li><li>         |</li><li>         +- <a class="l_k" href="functions/pack.html">pack</a></li><li>         |</li><li>         +- <span class="w">portable</span></li><li>         |</li><li>         +- <span class="w">recursion</span></li><li>         |</li><li>         +- <span class="w">redefine</span></li><li>         |</li><li>         +- <span class="w">redundant</span></li><li>         |</li><li>         +- <span class="w">regexp</span></li><li>         |</li><li>         +- <span class="w">severe</span> --------+</li><li>         |                 |</li><li>         |                 +- <span class="w">debugging</span></li><li>         |                 |</li><li>         |                 +- <span class="w">inplace</span></li><li>         |                 |</li><li>         |                 +- <span class="w">internal</span></li><li>         |                 |</li><li>         |                 +- <span class="w">malloc</span></li><li>         |</li><li>         +- <span class="w">signal</span></li><li>         |</li><li>         +- <a class="l_k" href="functions/substr.html">substr</a></li><li>         |</li><li>         +- <span class="w">syntax</span> --------+</li><li>         |                 |</li><li>         |                 +- <span class="w">ambiguous</span></li><li>         |                 |</li><li>         |                 +- <span class="w">bareword</span></li><li>         |                 |</li><li>         |                 +- <span class="w">digit</span></li><li>         |                 |</li><li>         |                 +- <span class="w">illegalproto</span></li><li>         |                 |</li><li>         |                 +- <span class="w">parenthesis</span></li><li>         |                 |</li><li>         |                 +- <span class="w">precedence</span></li><li>         |                 |</li><li>         |                 +- <a class="l_k" href="functions/printf.html">printf</a></li><li>         |                 |</li><li>         |                 +- <a class="l_k" href="functions/prototype.html">prototype</a></li><li>         |                 |</li><li>         |                 +- <span class="q">qw</span></li><li>         <span class="q">|                 |</span></li><li>         |                 +- <span class="w">reserved</span></li><li>         |                 |</li><li>         |                 +- <span class="w">semicolon</span></li><li>         |</li><li>         +- <span class="w">taint</span></li><li>         |</li><li>         +- <span class="w">threads</span></li><li>         |</li><li>         +- <span class="w">uninitialized</span></li><li>         |</li><li>         +- <a class="l_k" href="functions/unpack.html">unpack</a></li><li>         |</li><li>         +- <a class="l_k" href="functions/untie.html">untie</a></li><li>         |</li><li>         +- <span class="w">utf8</span> ----------+</li><li>         |                 |</li><li>         |                 +- <span class="w">non_unicode</span></li><li>         |                 |</li><li>         |                 +- <span class="w">nonchar</span></li><li>         |                 |</li><li>         |                 +- <span class="w">surrogate</span></li><li>         |</li><li>         +- <span class="w">void</span></li></ol></pre><p>Just like the "strict" pragma any of these categories can be combined</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(void redefine)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">qw(io syntax untie)</span><span class="sc">;</span></li></ol></pre><p>Also like the "strict" pragma, if there is more than one instance of the
<code class="inline"><span class="w">warnings</span></code>
 pragma in a given scope the cumulative effect is additive.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(void)</span><span class="sc">;</span> <span class="c"># only &quot;void&quot; warnings enabled</span></li><li>    ...</li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(io)</span><span class="sc">;</span>   <span class="c"># only &quot;void&quot; &amp; &quot;io&quot; warnings enabled</span></li><li>    ...</li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">qw(void)</span><span class="sc">;</span>  <span class="c"># only &quot;io&quot; warnings enabled</span></li></ol></pre><p>To determine which category a specific warning has been assigned to see
<a href="perldiag.html">perldiag</a>.</p>
<p>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.</p>
<p>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.</p>
<a name="Fatal-Warnings"></a><h2>Fatal Warnings
</h2>
<p>The presence of the word "FATAL" in the category list will escalate
warnings in those categories into fatal errors in that lexical scope.</p>
<p><b>NOTE:</b> FATAL warnings should be used with care, particularly
<code class="inline"><span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span></code>
.</p>
<p>Libraries using <a href="#FUNCTIONS">warnings::warn</a> for custom warning categories
generally don't expect <a href="#FUNCTIONS">warnings::warn</a> 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.</p>
<p>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
<a href="http://search.cpan.org/perldoc/http:#%2fwww.nntp.perl.org%2fgroup%2fperl.perl5.porters%2f2015%2f01%2fmsg225235.html">this perl5-porters post</a>.</p>
<p>While some developers find fatalizing some warnings to be a useful
defensive programming technique, using <code class="inline"><span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span></code>
 to fatalize
all possible warning categories -- including custom ones -- is particularly
risky.  Therefore, the use of <code class="inline"><span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span></code>
 is
<a href="perlpolicy.html#discouraged">discouraged</a>.</p>
<p>The <a href="http://search.cpan.org/perldoc/strictures#VERSION-2">strictures</a> module on CPAN offers one example of
a warnings subset that the module's authors believe is relatively safe to
fatalize.</p>
<p><b>NOTE:</b> users of FATAL warnings, especially those using
<code class="inline"><span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span></code>
, 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.</p>
<p>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.</p>
<p>In the code below, the use of <code class="inline"><a class="l_k" href="functions/time.html">time</a></code>, <code class="inline"><a class="l_k" href="functions/length.html">length</a></code>
and <code class="inline"><a class="l_k" href="functions/join.html">join</a></code> can all produce a <code class="inline"><span class="q">&quot;Useless use of xxx in void context&quot;</span></code>

warning.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/time.html">time</a><span class="sc">;</span></li><li></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">qw(void)</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/length.html">length</a> <span class="q">&quot;abc&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/join.html">join</a> <span class="q">&quot;&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="n">3</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;done\n&quot;</span><span class="sc">;</span></li></ol></pre><p>When run it produces this output</p>
<pre class="verbatim"><ol><li>    Useless use of time in void context at fatal line 3.</li><li>    Useless use of length in void context at fatal line 7.</li></ol></pre><p>The scope where <code class="inline"><a class="l_k" href="functions/length.html">length</a></code> is used has escalated the <code class="inline"><span class="w">void</span></code>
 warnings
category into a fatal error, so the program terminates immediately when it
encounters the warning.</p>
<p>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:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">qw(void)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">qw(void)</span><span class="sc">;</span></li></ol></pre><p>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.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span><span class="cm">,</span> <span class="w">NONFATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;syntax&#39;</span><span class="sc">;</span></li></ol></pre><p>As of Perl 5.20, instead of <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span><span class="sc">;</span></code>
 you can
use:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="functions/use.html">use</a> <span class="v">v5.20</span><span class="sc">;</span>       <span class="c"># Perl 5.20 or greater is required for the following</span></li><li>   <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;FATAL&#39;</span><span class="sc">;</span>  <span class="c"># short form of &quot;use warnings FATAL =&gt; &#39;all&#39;;&quot;</span></li></ol></pre><p>If you want your program to be compatible with versions of Perl before
5.20, you must use <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span><span class="sc">;</span></code>
 instead.  (In
previous versions of Perl, the behavior of the statements
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;FATAL&#39;</span><span class="sc">;</span></code>
, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;NONFATAL&#39;</span><span class="sc">;</span></code>
 and
<code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&#39;FATAL&#39;</span><span class="sc">;</span></code>
 was unspecified; they did not behave as if
they included the <code class="inline"><span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span></code>
 portion.  As of 5.20, they do.)</p>
<a name="Reporting-Warnings-from-a-Module"></a><h2>Reporting Warnings from a Module
 </h2>
<p>The <code class="inline"><span class="w">warnings</span></code>
 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 <code class="inline"><span class="w">warnings</span></code>

pragma.</p>
<p>Consider the module <code class="inline"><span class="w">MyMod::Abc</span></code>
 below.</p>
<pre class="verbatim"><ol><li><a name="package-MyMod::Abc"></a>    package <span class="i">MyMod::Abc</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></li><li></li><li><a name="open"></a>    sub <span class="m">open</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$path</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        if <span class="s">(</span><span class="i">$path</span> !~ <span class="q">m#^/#</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="i">warnings::warn</span><span class="s">(</span><span class="q">&quot;changing relative path to /var/abc&quot;</span><span class="s">)</span></li><li>                if <span class="i">warnings::enabled</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>            <span class="i">$path</span> = <span class="q">&quot;/var/abc/$path&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>The call to <code class="inline"><span class="w">warnings::register</span></code>
 will create a new warnings category
called "MyMod::Abc", i.e. the new category name matches the current
package name.  The <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> 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 <code class="inline"><span class="w">MyMod::Abc</span></code>
 has actually
enabled them with the <code class="inline"><span class="w">warnings</span></code>
 pragma like below.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;MyMod::Abc&#39;</span><span class="sc">;</span></li><li>    ...</li><li>    <span class="i">abc::open</span><span class="s">(</span><span class="q">&quot;../fred.txt&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>It is also possible to test whether the pre-defined warnings categories are
set in the calling module with the <code class="inline"><span class="w">warnings::enabled</span></code>
 function.  Consider
this snippet of code:</p>
<pre class="verbatim"><ol><li><a name="package-MyMod::Abc"></a>    package <span class="i">MyMod::Abc</span><span class="sc">;</span></li><li></li><li><a name="open"></a>    sub <span class="m">open</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="q">&quot;deprecated&quot;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="i">warnings::warn</span><span class="s">(</span><span class="q">&quot;deprecated&quot;</span><span class="cm">,</span></li><li>                           <span class="q">&quot;open is deprecated, use new instead&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="i">new</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="new"></a>    sub <span class="m">new</span></li><li>    ...</li><li>    <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>The function <code class="inline"><a class="l_k" href="functions/open.html">open</a></code> 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.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">&#39;deprecated&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span></li><li>    ...</li><li>    <span class="i">MyMod::Abc::open</span><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Either the <code class="inline"><span class="w">warnings::warn</span></code>
 or <code class="inline"><span class="w">warnings::warnif</span></code>
 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</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">&#39;MyMod::Abc&#39;</span><span class="sc">;</span></li><li>    ...</li><li>    <span class="i">MyMod::Abc::open</span><span class="s">(</span><span class="q">&#39;../fred.txt&#39;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>the <code class="inline"><span class="w">warnings::warnif</span></code>
 function will detect this and die after
displaying the warning message.</p>
<p>The three warnings functions, <code class="inline"><span class="w">warnings::warn</span></code>
, <code class="inline"><span class="w">warnings::warnif</span></code>

and <code class="inline"><span class="w">warnings::enabled</span></code>
 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.</p>
<p>Consider this example:</p>
<pre class="verbatim"><ol><li><a name="package-Original"></a>    package <span class="i">Original</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></li><li></li><li><a name="new"></a>    sub <span class="m">new</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="s">]</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="check"></a>    sub <span class="m">check</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>        if <span class="s">(</span><span class="i">$value</span> % <span class="n">2</span> &amp;&amp; <span class="i">warnings::enabled</span><span class="s">(</span><span class="i">$self</span><span class="s">)</span><span class="s">)</span></li><li>          <span class="s">{</span> <span class="i">warnings::warn</span><span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="q">&quot;Odd numbers are unsafe&quot;</span><span class="s">)</span> <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li><a name="doit"></a>    sub <span class="m">doit</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$self</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$value</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <span class="i">$self</span><span class="i">-&gt;check</span><span class="s">(</span><span class="i">$value</span><span class="s">)</span><span class="sc">;</span></li><li>        <span class="c"># ...</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="n">1</span><span class="sc">;</span></li><li></li><li><a name="package-Derived"></a>    package <span class="i">Derived</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Original</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/our.html">our</a> <span class="i">@ISA</span> = <span class="q">qw( Original )</span><span class="sc">;</span></li><li><a name="new-1"></a>    sub <span class="m">new</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$class</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/bless.html">bless</a> <span class="s">[</span><span class="s">]</span><span class="cm">,</span> <span class="i">$class</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li></li><li>    <span class="n">1</span><span class="sc">;</span></li></ol></pre><p>The code below makes use of both modules, but it only enables warnings from
<code class="inline"><span class="w">Derived</span></code>
.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Original</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Derived</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">'Derived'</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = <span class="w">Original</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$a</span><span class="i">-&gt;doit</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = <span class="w">Derived</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$a</span><span class="i">-&gt;doit</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When this code is run only the <code class="inline"><span class="w">Derived</span></code>
 object, <code class="inline"><span class="i">$b</span></code>
, will generate
a warning.</p>
<pre class="verbatim"><ol><li>    <span class="w">Odd</span> <span class="w">numbers</span> <span class="w">are</span> <span class="w">unsafe</span> <span class="w">at</span> <span class="w">main</span>.<span class="w">pl</span> <span class="w">line</span> <span class="n">7</span></li></ol></pre><p>Notice also that the warning is reported at the line where the object is first
used.</p>
<p>When registering new categories of warning, you can supply more names to
warnings::register like this:</p>
<pre class="verbatim"><ol><li><a name="package-MyModule"></a>    package <span class="i">MyModule</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span> <span class="q">qw(format precision)</span><span class="sc">;</span></li><li></li><li>    ...</li><li></li><li>    <span class="i">warnings::warnif</span><span class="s">(</span><span class="q">&#39;MyModule::format&#39;</span><span class="cm">,</span> <span class="q">&#39;...&#39;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="FUNCTIONS"></a><h1>FUNCTIONS</h1>
<ul>
<li><a name="use-warnings%3a%3aregister"></a><b>use warnings::register</b>
<p>Creates a new warnings category with the same name as the package where
the call to the pragma is used.</p>
</li>
<li><a name="warnings%3a%3aenabled()"></a><b>warnings::enabled()</b>
<p>Use the warnings category with the same name as the current package.</p>
<p>Return TRUE if that warnings category is enabled in the calling module.
Otherwise returns FALSE.</p>
</li>
<li><a name="warnings%3a%3aenabled(%24category)"></a><b>warnings::enabled($category)</b>
<p>Return TRUE if the warnings category, <code class="inline"><span class="i">$category</span></code>
, is enabled in the
calling module.
Otherwise returns FALSE.</p>
</li>
<li><a name="warnings%3a%3aenabled(%24object)"></a><b>warnings::enabled($object)</b>
<p>Use the name of the class for the object reference, <code class="inline"><span class="i">$object</span></code>
, as the
warnings category.</p>
<p>Return TRUE if that warnings category is enabled in the first scope
where the object is used.
Otherwise returns FALSE.</p>
</li>
<li><a name="warnings%3a%3afatal_enabled()"></a><b>warnings::fatal_enabled()</b>
<p>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.</p>
</li>
<li><a name="warnings%3a%3afatal_enabled(%24category)"></a><b>warnings::fatal_enabled($category)</b>
<p>Return TRUE if the warnings category <code class="inline"><span class="i">$category</span></code>
 has been set to FATAL in
the calling module.
Otherwise returns FALSE.</p>
</li>
<li><a name="warnings%3a%3afatal_enabled(%24object)"></a><b>warnings::fatal_enabled($object)</b>
<p>Use the name of the class for the object reference, <code class="inline"><span class="i">$object</span></code>
, as the
warnings category.</p>
<p>Return TRUE if that warnings category has been set to FATAL in the first
scope where the object is used.
Otherwise returns FALSE.</p>
</li>
<li><a name="warnings%3a%3awarn(%24message)"></a><b>warnings::warn($message)</b>
<p>Print <code class="inline"><span class="i">$message</span></code>
 to STDERR.</p>
<p>Use the warnings category with the same name as the current package.</p>
<p>If that warnings category has been set to "FATAL" in the calling module
then die. Otherwise return.</p>
</li>
<li><a name="warnings%3a%3awarn(%24category%2c-%24message)"></a><b>warnings::warn($category, $message)</b>
<p>Print <code class="inline"><span class="i">$message</span></code>
 to STDERR.</p>
<p>If the warnings category, <code class="inline"><span class="i">$category</span></code>
, has been set to "FATAL" in the
calling module then die. Otherwise return.</p>
</li>
<li><a name="warnings%3a%3awarn(%24object%2c-%24message)"></a><b>warnings::warn($object, $message)</b>
<p>Print <code class="inline"><span class="i">$message</span></code>
 to STDERR.</p>
<p>Use the name of the class for the object reference, <code class="inline"><span class="i">$object</span></code>
, as the
warnings category.</p>
<p>If that warnings category has been set to "FATAL" in the scope where <code class="inline"><span class="i">$object</span></code>

is first used then die. Otherwise return.</p>
</li>
<li><a name="warnings%3a%3awarnif(%24message)"></a><b>warnings::warnif($message)</b>
<p>Equivalent to:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="s">)</span><span class="s">)</span></li><li>      <span class="s">{</span> <span class="i">warnings::warn</span><span class="s">(</span><span class="i">$message</span><span class="s">)</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="warnings%3a%3awarnif(%24category%2c-%24message)"></a><b>warnings::warnif($category, $message)</b>
<p>Equivalent to:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="i">$category</span><span class="s">)</span><span class="s">)</span></li><li>      <span class="s">{</span> <span class="i">warnings::warn</span><span class="s">(</span><span class="i">$category</span><span class="cm">,</span> <span class="i">$message</span><span class="s">)</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="warnings%3a%3awarnif(%24object%2c-%24message)"></a><b>warnings::warnif($object, $message)</b>
<p>Equivalent to:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">warnings::enabled</span><span class="s">(</span><span class="i">$object</span><span class="s">)</span><span class="s">)</span></li><li>      <span class="s">{</span> <span class="i">warnings::warn</span><span class="s">(</span><span class="i">$object</span><span class="cm">,</span> <span class="i">$message</span><span class="s">)</span> <span class="s">}</span></li></ol></pre></li>
<li><a name="warnings%3a%3aregister_categories(%40names)"></a><b>warnings::register_categories(@names)</b>
<p>This registers warning categories for the given names and is primarily for
use by the warnings::register pragma.</p>
</li>
</ul>
<p>See also <a href="perlmodlib.html#Pragmatic-Modules">Pragmatic Modules in perlmodlib</a> and <a href="perldiag.html">perldiag</a>.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Default-Warnings-and-Optional-Warnings">Default Warnings and Optional Warnings</a><li><a href="#What's-wrong-with-*-w*-and-%24%5eW">What's wrong with *-w* and $^W</a><li><a href="#Controlling-Warnings-from-the-Command-Line">Controlling Warnings from the Command Line</a><li><a href="#Backward-Compatibility">Backward Compatibility</a><li><a href="#Category-Hierarchy">Category Hierarchy
</a><li><a href="#Fatal-Warnings">Fatal Warnings
</a><li><a href="#Reporting-Warnings-from-a-Module">Reporting Warnings from a Module
 </a></ul><li><a href="#FUNCTIONS">FUNCTIONS</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'warnings';
  perldoc.pageAddress = 'warnings.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>