~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to perllexwarn.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-language.html">Language reference</a> &gt; perllexwarn</div>
83
86
      <script language="JavaScript">fromSearch();</script>
84
 
      <div id="contentBody"><div class="title_container"><div class="page_title">perllexwarn</div></div><ul><li><a href="#NAME-warning%2c-lexical-warnings-warning">NAME
 
87
      <div id="contentBody"><div class="title_container"><div class="page_title">perllexwarn</div></div><ul><li><a href="#NAME">NAME
85
88
  </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 <b>-w</b> and <code class="inline"><span class="i">$^W</span></code>
86
 
</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-warning%2c-categories">Category Hierarchy
87
 
</a><li><a href="#Fatal-Warnings-warning%2c-fatal">Fatal Warnings
88
 
</a><li><a href="#Reporting-Warnings-from-a-Module-warning%2c-reporting-warning%2c-registering">Reporting Warnings from a Module
89
 
 </a></ul><li><a href="#TODO">TODO</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME-warning%2c-lexical-warnings-warning"></a><h1>NAME
 
89
</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
 
90
</a><li><a href="#Fatal-Warnings">Fatal Warnings
 
91
</a><li><a href="#Reporting-Warnings-from-a-Module">Reporting Warnings from a Module
 
92
 </a></ul><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME
90
93
  </h1>
91
94
<p>perllexwarn - Perl Lexical Warnings</p>
92
95
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
93
 
<p>The <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a></code>
94
 
 pragma is a replacement for both the command line
95
 
flag <b>-w</b> and the equivalent Perl variable, <code class="inline"><span class="i">$^W</span></code>
 
96
<p>The <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
 
97
 pragma enables to control precisely what warnings are
 
98
to be enabled in which parts of a Perl program. It's a more flexible
 
99
alternative for both the command line flag <b>-w</b> and the equivalent Perl
 
100
variable, <code class="inline"><span class="i">$^W</span></code>
96
101
.</p>
97
 
<p>The pragma works just like the existing "strict" pragma.
 
102
<p>This pragma works just like the <code class="inline"><span class="w">strict</span></code>
 
103
 pragma.
98
104
This means that the scope of the warning pragma is limited to the
99
105
enclosing block. It also means that the pragma setting will not
100
106
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
103
109
<p>By default, optional warnings are disabled, so any legacy code that
104
110
doesn't attempt to control the warnings will work unchanged.</p>
105
111
<p>All warnings are enabled in a block by either of these:</p>
106
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
107
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">&#39;all&#39;</span><span class="sc">;</span></pre>
 
112
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span>
 
113
    <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></pre>
108
114
<p>Similarly all warnings are disabled in a block by either of these:</p>
109
 
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
110
 
    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">&#39;all&#39;</span><span class="sc">;</span></pre>
 
115
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>
 
116
    <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></pre>
111
117
<p>For example, consider the code below:</p>
112
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
 
118
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span>
113
119
    <a class="l_k" href="functions/my.html">my</a> <span class="i">@a</span><span class="sc">;</span>
114
120
    <span class="s">{</span>
115
 
        <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
 
121
        <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>
116
122
        <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>
117
123
    <span class="s">}</span>
118
124
    <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></pre>
125
131
 will not.</p>
126
132
<a name="Default-Warnings-and-Optional-Warnings"></a><h2>Default Warnings and Optional Warnings</h2>
127
133
<p>Before the introduction of lexical warnings, Perl had two classes of
128
 
warnings: mandatory and optional. </p>
 
134
warnings: mandatory and optional.</p>
129
135
<p>As its name suggests, if your code tripped a mandatory warning, you
130
136
would get a warning whether you wanted it or not.
131
137
For example, the code below would always produce an <code class="inline"><span class="q">&quot;isn&#39;t numeric&quot;</span></code>
141
147
be reported for the <code class="inline"><span class="i">$a</span></code>
142
148
 variable.</p>
143
149
<pre class="verbatim">    <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>
144
 
    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
 
150
    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>
145
151
    <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></pre>
146
152
<p>Note that neither the <b>-w</b> flag or the <code class="inline"><span class="i">$^W</span></code>
147
153
 can be used to
178
184
 is the way you can inadvertently
179
185
change the warning setting in unexpected places in your code. For example,
180
186
when the code below is run (without the <b>-w</b> flag), the second call
181
 
to <code class="inline">doit</code>
 
187
to <code class="inline"><span class="w">doit</span></code>
182
188
 will trip a <code class="inline"><span class="q">&quot;Use of uninitialized value&quot;</span></code>
183
189
 warning, whereas
184
190
the first will not.</p>
199
205
<p>There are three Command Line flags that can be used to control when
200
206
warnings are (or aren't) produced:</p>
201
207
<ul>
202
 
<li><a name="*-w*--w"></a><b><b>-w</b>
 
208
<li><a name="*-w*"></a><b><b>-w</b>
203
209
</b>
204
210
<p>This is  the existing flag. If the lexical warnings pragma is <b>not</b>
205
211
used in any of you code, or any of the modules that you use, this flag
206
212
will enable warnings everywhere. See <a href="#Backward-Compatibility">"Backward Compatibility"</a> for
207
213
details of how this flag interacts with lexical warnings.</p>
208
214
</li>
209
 
<li><a name="*-W*--W"></a><b><b>-W</b>
 
215
<li><a name="*-W*"></a><b><b>-W</b>
210
216
</b>
211
217
<p>If the <b>-W</b> flag is used on the command line, it will enable all warnings
212
218
throughout the program regardless of whether warnings were disabled
213
 
locally using <code class="inline"><a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a></code>
 
219
locally using <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span></code>
214
220
 or <code class="inline"><span class="i">$^W</span> =<span class="n">0</span></code>
215
221
. This includes all files that get
216
222
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>.
217
223
Think of it as the Perl equivalent of the "lint" command.</p>
218
224
</li>
219
 
<li><a name="*-X*--X"></a><b><b>-X</b>
 
225
<li><a name="*-X*"></a><b><b>-X</b>
220
226
</b>
221
227
<p>Does the exact opposite to the <b>-W</b> flag, i.e. it disables all warnings.</p>
222
228
</li>
232
238
<li>
233
239
<p>If none of the three command line flags (<b>-w</b>, <b>-W</b> or <b>-X</b>) that
234
240
control warnings is used and neither <code class="inline"><span class="i">$^W</span></code>
235
 
 or the <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma
 
241
 or the <code class="inline"><span class="w">warnings</span></code>
 
242
 pragma
236
243
are used, then default warnings will be enabled and optional warnings
237
244
disabled.
238
245
This means that legacy code that doesn't attempt to control the warnings
243
250
 variable as in 5.005 -- this
244
251
means that any legacy code that currently relies on manipulating <code class="inline"><span class="i">$^W</span></code>
245
252
 
246
 
to control warning behavior will still work as is. </p>
 
253
to control warning behavior will still work as is.</p>
247
254
</li>
248
255
<li>
249
256
<p>Apart from now being a boolean, the <code class="inline"><span class="i">$^W</span></code>
252
259
disable/enable default warnings.</p>
253
260
</li>
254
261
<li>
255
 
<p>If a piece of code is under the control of the <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma,
 
262
<p>If a piece of code is under the control of the <code class="inline"><span class="w">warnings</span></code>
 
263
 pragma,
256
264
both the <code class="inline"><span class="i">$^W</span></code>
257
265
 variable and the <b>-w</b> flag will be ignored for the
258
266
scope of the lexical warning.</p>
263
271
</li>
264
272
</ol>
265
273
<p>The combined effect of 3 &amp; 4 is that it will allow code which uses
266
 
the <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma to control the warning behavior of $^W-type
 
274
the <code class="inline"><span class="w">warnings</span></code>
 
275
 pragma to control the warning behavior of $^W-type
267
276
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>
268
277
) if it really wants to, but not vice-versa.</p>
269
 
<a name="Category-Hierarchy-warning%2c-categories"></a><h2>Category Hierarchy
 
278
<a name="Category-Hierarchy"></a><h2>Category Hierarchy
270
279
</h2>
271
280
<p>A hierarchy of "categories" have been defined to allow groups of warnings
272
281
to be enabled/disabled in isolation.</p>
361
370
       |
362
371
       +- utf8
363
372
       |
364
 
       +- void
365
 
       |
366
 
       +- y2k</pre><p>Just like the "strict" pragma any of these categories can be combined</p>
367
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(void redefine)</span><span class="sc">;</span>
368
 
    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(io syntax untie)</span><span class="sc">;</span></pre>
 
373
       +- void</pre><p>Just like the "strict" pragma any of these categories can be combined</p>
 
374
<pre class="verbatim">    <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>
 
375
    <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></pre>
369
376
<p>Also like the "strict" pragma, if there is more than one instance of the
370
 
<code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma in a given scope the cumulative effect is additive. </p>
371
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(void)</span><span class="sc">;</span> <span class="c"># only &quot;void&quot; warnings enabled</span>
372
 
    ...
373
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(io)</span><span class="sc">;</span>   <span class="c"># only &quot;void&quot; &amp; &quot;io&quot; warnings enabled</span>
374
 
    ...
375
 
    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(void)</span><span class="sc">;</span>  <span class="c"># only &quot;io&quot; warnings enabled</span></pre>
 
377
<code class="inline"><span class="w">warnings</span></code>
 
378
 pragma in a given scope the cumulative effect is additive.</p>
 
379
<pre class="verbatim">    <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>
 
380
    ...
 
381
    <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>
 
382
    ...
 
383
    <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></pre>
376
384
<p>To determine which category a specific warning has been assigned to see
377
385
<a href="perldiag.html">perldiag</a>.</p>
378
386
<p>Note: In Perl 5.6.1, the lexical warnings category "deprecated" was a
379
387
sub-category of the "syntax" category. It is now a top-level category
380
388
in its own right.</p>
381
 
<a name="Fatal-Warnings-warning%2c-fatal"></a><h2>Fatal Warnings
 
389
<a name="Fatal-Warnings"></a><h2>Fatal Warnings
382
390
</h2>
383
391
<p>The presence of the word "FATAL" in the category list will escalate any
384
392
warnings detected from the categories specified in the lexical scope
386
394
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>
387
395
 
388
396
warning.</p>
389
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span></pre>
 
397
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></pre>
390
398
<pre class="verbatim">    <a class="l_k" href="functions/time.html">time</a><span class="sc">;</span></pre>
391
399
<pre class="verbatim">    <span class="s">{</span>
392
 
        <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> FATAL <span class="cm">=&gt;</span> <span class="q">qw(void)</span><span class="sc">;</span>
 
400
        <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>
393
401
        <a class="l_k" href="functions/length.html">length</a> <span class="q">&quot;abc&quot;</span><span class="sc">;</span>
394
402
    <span class="s">}</span></pre>
395
403
<pre class="verbatim">    <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></pre>
396
404
<pre class="verbatim">    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;done\n&quot;</span><span class="sc">;</span></pre>
397
405
<p>When run it produces this output</p>
398
406
<pre class="verbatim">    Useless use of time in void context at fatal line 3.
399
 
    Useless use of length in void context at fatal line 7.  </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">void</code>
 
407
    Useless use of length in void context at fatal line 7.</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>
400
408
 warnings
401
409
category into a fatal error, so the program terminates immediately it
402
410
encounters the warning.</p>
403
411
<p>To explicitly turn off a "FATAL" warning you just disable the warning
404
412
it is associated with.  So, for example, to disable the "void" warning
405
413
in the example above, either of these will do the trick:</p>
406
 
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">qw(void)</span><span class="sc">;</span>
407
 
    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a> FATAL <span class="cm">=&gt;</span> <span class="q">qw(void)</span><span class="sc">;</span></pre>
 
414
<pre class="verbatim">    <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>
 
415
    <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></pre>
408
416
<p>If you want to downgrade a warning that has been escalated into a fatal
409
417
error back to a normal warning, you can use the "NONFATAL" keyword. For
410
418
example, the code below will promote all warnings into fatal errors,
411
419
except for those in the "syntax" category.</p>
412
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> FATAL <span class="cm">=&gt;</span> <span class="q">&#39;all&#39;</span><span class="cm">,</span> NONFATAL <span class="cm">=&gt;</span> <span class="q">&#39;syntax&#39;</span><span class="sc">;</span></pre>
413
 
<a name="Reporting-Warnings-from-a-Module-warning%2c-reporting-warning%2c-registering"></a><h2>Reporting Warnings from a Module
 
420
<pre class="verbatim">    <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></pre>
 
421
<a name="Reporting-Warnings-from-a-Module"></a><h2>Reporting Warnings from a Module
414
422
 </h2>
415
 
<p>The <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma provides a number of functions that are useful for
 
423
<p>The <code class="inline"><span class="w">warnings</span></code>
 
424
 pragma provides a number of functions that are useful for
416
425
module authors. These are used when you want to report a module-specific
417
 
warning to a calling module has enabled warnings via the <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code>
 
426
warning to a calling module has enabled warnings via the <code class="inline"><span class="w">warnings</span></code>
 
427
 
418
428
pragma.</p>
419
 
<p>Consider the module <code class="inline">MyMod::Abc</code>
 
429
<p>Consider the module <code class="inline"><span class="w">MyMod::Abc</span></code>
420
430
 below.</p>
421
431
<pre class="verbatim"><a name="package-MyMod::Abc"></a>    package <span class="i">MyMod::Abc</span><span class="sc">;</span></pre>
422
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings/register.html">warnings::register</a><span class="sc">;</span></pre>
 
432
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></pre>
423
433
<pre class="verbatim"><a name="open"></a>    sub <span class="m">open</span> <span class="s">{</span>
424
434
        <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>
425
435
        if <span class="s">(</span><span class="i">$path</span> !~ <span class="q">m#^/#</span><span class="s">)</span> <span class="s">{</span>
429
439
        <span class="s">}</span>
430
440
    <span class="s">}</span></pre>
431
441
<pre class="verbatim">    <span class="n">1</span><span class="sc">;</span></pre>
432
 
<p>The call to <code class="inline"><a class="l_w" href="warnings/register.html">warnings::register</a></code> will create a new warnings category
 
442
<p>The call to <code class="inline"><span class="w">warnings::register</span></code>
 
443
 will create a new warnings category
433
444
called "MyMod::abc", i.e. the new category name matches the current
434
445
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
435
446
message if it gets given a relative path as a parameter. This warnings
436
 
will only be displayed if the code that uses <code class="inline">MyMod::Abc</code>
 
447
will only be displayed if the code that uses <code class="inline"><span class="w">MyMod::Abc</span></code>
437
448
 has actually
438
 
enabled them with the <code class="inline"><a class="l_w" href="warnings.html">warnings</a></code> pragma like below.</p>
439
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> MyMod::Abc<span class="sc">;</span>
440
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">&#39;MyMod::Abc&#39;</span><span class="sc">;</span>
 
449
enabled them with the <code class="inline"><span class="w">warnings</span></code>
 
450
 pragma like below.</p>
 
451
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span>
 
452
    <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>
441
453
    ...
442
454
    <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></pre>
443
455
<p>It is also possible to test whether the pre-defined warnings categories are
444
 
set in the calling module with the <code class="inline">warnings::enabled</code>
 
456
set in the calling module with the <code class="inline"><span class="w">warnings::enabled</span></code>
445
457
 function. Consider
446
458
this snippet of code:</p>
447
459
<pre class="verbatim"><a name="package-MyMod::Abc"></a>    package <span class="i">MyMod::Abc</span><span class="sc">;</span></pre>
455
467
    1;</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
456
468
display a warning message whenever the calling module has (at least) the
457
469
"deprecated" warnings category enabled. Something like this, say.</p>
458
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">&#39;deprecated&#39;</span><span class="sc">;</span>
459
 
    <a class="l_k" href="functions/use.html">use</a> MyMod::Abc<span class="sc">;</span>
 
470
<pre class="verbatim">    <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>
 
471
    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span>
460
472
    ...
461
473
    <span class="i">MyMod::Abc::open</span><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></pre>
462
 
<p>Either the <code class="inline">warnings::warn</code>
463
 
 or <code class="inline">warnings::warnif</code>
 
474
<p>Either the <code class="inline"><span class="w">warnings::warn</span></code>
 
475
 or <code class="inline"><span class="w">warnings::warnif</span></code>
464
476
 function should be
465
477
used to actually display the warnings message. This is because they can
466
478
make use of the feature that allows warnings to be escalated into fatal
467
479
errors. So in this case</p>
468
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> MyMod::Abc<span class="sc">;</span>
469
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> FATAL <span class="cm">=&gt;</span> <span class="q">&#39;MyMod::Abc&#39;</span><span class="sc">;</span>
 
480
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">MyMod::Abc</span><span class="sc">;</span>
 
481
    <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>
470
482
    ...
471
483
    <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></pre>
472
 
<p>the <code class="inline">warnings::warnif</code>
 
484
<p>the <code class="inline"><span class="w">warnings::warnif</span></code>
473
485
 function will detect this and die after
474
486
displaying the warning message.</p>
475
 
<p>The three warnings functions, <code class="inline">warnings::warn</code>
476
 
, <code class="inline">warnings::warnif</code>
 
487
<p>The three warnings functions, <code class="inline"><span class="w">warnings::warn</span></code>
 
488
, <code class="inline"><span class="w">warnings::warnif</span></code>
477
489
 
478
 
and <code class="inline">warnings::enabled</code>
 
490
and <code class="inline"><span class="w">warnings::enabled</span></code>
479
491
 can optionally take an object reference in place
480
492
of a category name. In this case the functions will use the class name
481
493
of the object as the warnings category.</p>
482
494
<p>Consider this example:</p>
483
495
<pre class="verbatim"><a name="package-Original"></a>    package <span class="i">Original</span><span class="sc">;</span></pre>
484
 
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a><span class="sc">;</span>
485
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings/register.html">warnings::register</a><span class="sc">;</span></pre>
 
496
<pre class="verbatim">    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>
 
497
    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span></pre>
486
498
<pre class="verbatim"><a name="new"></a>    sub <span class="m">new</span>
487
499
    <span class="s">{</span>
488
500
        <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>
502
514
    <span class="s">}</span></pre>
503
515
<pre class="verbatim">    <span class="n">1</span><span class="sc">;</span></pre>
504
516
<pre class="verbatim"><a name="package-Derived"></a>    package <span class="i">Derived</span><span class="sc">;</span></pre>
505
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings/register.html">warnings::register</a><span class="sc">;</span>
506
 
    <a class="l_k" href="functions/use.html">use</a> Original<span class="sc">;</span>
 
517
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings::register</span><span class="sc">;</span>
 
518
    <a class="l_k" href="functions/use.html">use</a> <span class="w">Original</span><span class="sc">;</span>
507
519
    <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>
508
520
<a name="new"></a>    sub <span class="m">new</span>
509
521
    <span class="s">{</span>
512
524
    <span class="s">}</span></pre>
513
525
<pre class="verbatim">    <span class="n">1</span><span class="sc">;</span></pre>
514
526
<p>The code below makes use of both modules, but it only enables warnings from 
515
 
<code class="inline">Derived</code>
 
527
<code class="inline"><span class="w">Derived</span></code>
516
528
.</p>
517
 
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> Original<span class="sc">;</span>
518
 
    <a class="l_k" href="functions/use.html">use</a> Derived<span class="sc">;</span>
519
 
    <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a> <span class="q">&#39;Derived&#39;</span><span class="sc">;</span>
520
 
    <a class="l_k" href="functions/my.html">my</a> <span class="i">$a</span> = new Original<span class="sc">;</span>
 
529
<pre class="verbatim">    <a class="l_k" href="functions/use.html">use</a> <span class="w">Original</span><span class="sc">;</span>
 
530
    <a class="l_k" href="functions/use.html">use</a> <span class="w">Derived</span><span class="sc">;</span>
 
531
    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="q">'Derived'</span><span class="sc">;</span>
 
532
    <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>
521
533
    <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>
522
 
    <a class="l_k" href="functions/my.html">my</a> <span class="i">$b</span> = new Derived<span class="sc">;</span>
 
534
    <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>
523
535
    <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></pre>
524
 
<p>When this code is run only the <code class="inline">Derived</code>
 
536
<p>When this code is run only the <code class="inline"><span class="w">Derived</span></code>
525
537
 object, <code class="inline"><span class="i">$b</span></code>
526
538
, will generate
527
 
a warning. </p>
528
 
<pre class="verbatim">    Odd numbers are unsafe at main.pl line <span class="n">7</span></pre>
 
539
a warning.</p>
 
540
<pre class="verbatim">    <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></pre>
529
541
<p>Notice also that the warning is reported at the line where the object is first
530
542
used.</p>
531
 
<a name="TODO"></a><h1>TODO</h1>
532
 
<pre class="verbatim">  perl5db.pl
533
 
    The debugger saves and restores C&lt;$^W&gt; at runtime. I haven't checked
534
 
    whether the debugger will still work with the lexical warnings
535
 
    patch applied.</pre><pre class="verbatim">  <a class="l_w" href="diagnostics.html">diagnostics</a>.pm
536
 
    I *think* I&#39;ve got <a class="l_w" href="diagnostics.html">diagnostics</a> to work with the lexical <a class="l_w" href="warnings.html">warnings</a>
537
 
    patch<span class="cm">,</span> but there were design decisions made in <a class="l_w" href="diagnostics.html">diagnostics</a> to work
538
 
    around the limitations of C<span class="q">&lt;$^W&gt;</span>. Now that those limitations are gone<span class="cm">,</span>
539
 
    the module should be revisited.</pre>
540
 
<pre class="verbatim">  document calling the warnings::* functions from XS</pre>
541
543
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
542
544
<p><a href="warnings.html">warnings</a>, <a href="perldiag.html">perldiag</a>.</p>
543
545
<a name="AUTHOR"></a><h1>AUTHOR</h1>
559
561
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
560
562
        </form>
561
563
      </p>
 
564
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
562
565
      <h2>Labels:</h2>
563
566
      <p>
564
567
        <a href="#" onClick="addLabel('perllexwarn','perllexwarn.html')">Add this page</a>