| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "PERLLEXWARN 1" |
| 132 | .TH PERLLEXWARN 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | .IX Xref "warning, lexical warnings warning" |
| 135 | perllexwarn \- Perl Lexical Warnings |
| 136 | .SH "DESCRIPTION" |
| 137 | .IX Header "DESCRIPTION" |
| 138 | The \f(CW\*(C`use warnings\*(C'\fR pragma is a replacement for both the command line |
| 139 | flag \fB\-w\fR and the equivalent Perl variable, \f(CW$^W\fR. |
| 140 | .PP |
| 141 | The pragma works just like the existing \*(L"strict\*(R" pragma. |
| 142 | This means that the scope of the warning pragma is limited to the |
| 143 | enclosing block. It also means that the pragma setting will not |
| 144 | leak across files (via \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR). This allows |
| 145 | authors to independently define the degree of warning checks that will |
| 146 | be applied to their module. |
| 147 | .PP |
| 148 | By default, optional warnings are disabled, so any legacy code that |
| 149 | doesn't attempt to control the warnings will work unchanged. |
| 150 | .PP |
| 151 | All warnings are enabled in a block by either of these: |
| 152 | .PP |
| 153 | .Vb 2 |
| 154 | \& use warnings; |
| 155 | \& use warnings 'all'; |
| 156 | .Ve |
| 157 | .PP |
| 158 | Similarly all warnings are disabled in a block by either of these: |
| 159 | .PP |
| 160 | .Vb 2 |
| 161 | \& no warnings; |
| 162 | \& no warnings 'all'; |
| 163 | .Ve |
| 164 | .PP |
| 165 | For example, consider the code below: |
| 166 | .PP |
| 167 | .Vb 7 |
| 168 | \& use warnings; |
| 169 | \& my @a; |
| 170 | \& { |
| 171 | \& no warnings; |
| 172 | \& my $b = @a[0]; |
| 173 | \& } |
| 174 | \& my $c = @a[0]; |
| 175 | .Ve |
| 176 | .PP |
| 177 | The code in the enclosing block has warnings enabled, but the inner |
| 178 | block has them disabled. In this case that means the assignment to the |
| 179 | scalar \f(CW$c\fR will trip the \f(CW"Scalar value @a[0] better written as $a[0]"\fR |
| 180 | warning, but the assignment to the scalar \f(CW$b\fR will not. |
| 181 | .Sh "Default Warnings and Optional Warnings" |
| 182 | .IX Subsection "Default Warnings and Optional Warnings" |
| 183 | Before the introduction of lexical warnings, Perl had two classes of |
| 184 | warnings: mandatory and optional. |
| 185 | .PP |
| 186 | As its name suggests, if your code tripped a mandatory warning, you |
| 187 | would get a warning whether you wanted it or not. |
| 188 | For example, the code below would always produce an \f(CW"isn't numeric"\fR |
| 189 | warning about the \*(L"2:\*(R". |
| 190 | .PP |
| 191 | .Vb 1 |
| 192 | \& my $a = "2:" + 3; |
| 193 | .Ve |
| 194 | .PP |
| 195 | With the introduction of lexical warnings, mandatory warnings now become |
| 196 | \&\fIdefault\fR warnings. The difference is that although the previously |
| 197 | mandatory warnings are still enabled by default, they can then be |
| 198 | subsequently enabled or disabled with the lexical warning pragma. For |
| 199 | example, in the code below, an \f(CW"isn't numeric"\fR warning will only |
| 200 | be reported for the \f(CW$a\fR variable. |
| 201 | .PP |
| 202 | .Vb 3 |
| 203 | \& my $a = "2:" + 3; |
| 204 | \& no warnings; |
| 205 | \& my $b = "2:" + 3; |
| 206 | .Ve |
| 207 | .PP |
| 208 | Note that neither the \fB\-w\fR flag or the \f(CW$^W\fR can be used to |
| 209 | disable/enable default warnings. They are still mandatory in this case. |
| 210 | .ie n .Sh "What's wrong with \fB\-w\fP and $^W" |
| 211 | .el .Sh "What's wrong with \fB\-w\fP and \f(CW$^W\fP" |
| 212 | .IX Subsection "What's wrong with -w and $^W" |
| 213 | Although very useful, the big problem with using \fB\-w\fR on the command |
| 214 | line to enable warnings is that it is all or nothing. Take the typical |
| 215 | scenario when you are writing a Perl program. Parts of the code you |
| 216 | will write yourself, but it's very likely that you will make use of |
| 217 | pre-written Perl modules. If you use the \fB\-w\fR flag in this case, you |
| 218 | end up enabling warnings in pieces of code that you haven't written. |
| 219 | .PP |
| 220 | Similarly, using \f(CW$^W\fR to either disable or enable blocks of code is |
| 221 | fundamentally flawed. For a start, say you want to disable warnings in |
| 222 | a block of code. You might expect this to be enough to do the trick: |
| 223 | .PP |
| 224 | .Vb 5 |
| 225 | \& { |
| 226 | \& local ($^W) = 0; |
| 227 | \& my $a =+ 2; |
| 228 | \& my $b; chop $b; |
| 229 | \& } |
| 230 | .Ve |
| 231 | .PP |
| 232 | When this code is run with the \fB\-w\fR flag, a warning will be produced |
| 233 | for the \f(CW$a\fR line \*(-- \f(CW"Reversed += operator"\fR. |
| 234 | .PP |
| 235 | The problem is that Perl has both compile-time and run-time warnings. To |
| 236 | disable compile-time warnings you need to rewrite the code like this: |
| 237 | .PP |
| 238 | .Vb 5 |
| 239 | \& { |
| 240 | \& BEGIN { $^W = 0 } |
| 241 | \& my $a =+ 2; |
| 242 | \& my $b; chop $b; |
| 243 | \& } |
| 244 | .Ve |
| 245 | .PP |
| 246 | The other big problem with \f(CW$^W\fR is the way you can inadvertently |
| 247 | change the warning setting in unexpected places in your code. For example, |
| 248 | when the code below is run (without the \fB\-w\fR flag), the second call |
| 249 | to \f(CW\*(C`doit\*(C'\fR will trip a \f(CW"Use of uninitialized value"\fR warning, whereas |
| 250 | the first will not. |
| 251 | .PP |
| 252 | .Vb 4 |
| 253 | \& sub doit |
| 254 | \& { |
| 255 | \& my $b; chop $b; |
| 256 | \& } |
| 257 | .Ve |
| 258 | .PP |
| 259 | .Vb 1 |
| 260 | \& doit(); |
| 261 | .Ve |
| 262 | .PP |
| 263 | .Vb 4 |
| 264 | \& { |
| 265 | \& local ($^W) = 1; |
| 266 | \& doit() |
| 267 | \& } |
| 268 | .Ve |
| 269 | .PP |
| 270 | This is a side-effect of \f(CW$^W\fR being dynamically scoped. |
| 271 | .PP |
| 272 | Lexical warnings get around these limitations by allowing finer control |
| 273 | over where warnings can or can't be tripped. |
| 274 | .Sh "Controlling Warnings from the Command Line" |
| 275 | .IX Subsection "Controlling Warnings from the Command Line" |
| 276 | There are three Command Line flags that can be used to control when |
| 277 | warnings are (or aren't) produced: |
| 278 | .IP "\fB\-w\fR" 5 |
| 279 | .IX Xref "-w" |
| 280 | .IX Item "-w" |
| 281 | This is the existing flag. If the lexical warnings pragma is \fBnot\fR |
| 282 | used in any of you code, or any of the modules that you use, this flag |
| 283 | will enable warnings everywhere. See \*(L"Backward Compatibility\*(R" for |
| 284 | details of how this flag interacts with lexical warnings. |
| 285 | .IP "\fB\-W\fR" 5 |
| 286 | .IX Xref "-W" |
| 287 | .IX Item "-W" |
| 288 | If the \fB\-W\fR flag is used on the command line, it will enable all warnings |
| 289 | throughout the program regardless of whether warnings were disabled |
| 290 | locally using \f(CW\*(C`no warnings\*(C'\fR or \f(CW\*(C`$^W =0\*(C'\fR. This includes all files that get |
| 291 | included via \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR. |
| 292 | Think of it as the Perl equivalent of the \*(L"lint\*(R" command. |
| 293 | .IP "\fB\-X\fR" 5 |
| 294 | .IX Xref "-X" |
| 295 | .IX Item "-X" |
| 296 | Does the exact opposite to the \fB\-W\fR flag, i.e. it disables all warnings. |
| 297 | .Sh "Backward Compatibility" |
| 298 | .IX Subsection "Backward Compatibility" |
| 299 | If you are used with working with a version of Perl prior to the |
| 300 | introduction of lexically scoped warnings, or have code that uses both |
| 301 | lexical warnings and \f(CW$^W\fR, this section will describe how they interact. |
| 302 | .PP |
| 303 | How Lexical Warnings interact with \fB\-w\fR/\f(CW$^W\fR: |
| 304 | .IP "1." 5 |
| 305 | If none of the three command line flags (\fB\-w\fR, \fB\-W\fR or \fB\-X\fR) that |
| 306 | control warnings is used and neither \f(CW$^W\fR or the \f(CW\*(C`warnings\*(C'\fR pragma |
| 307 | are used, then default warnings will be enabled and optional warnings |
| 308 | disabled. |
| 309 | This means that legacy code that doesn't attempt to control the warnings |
| 310 | will work unchanged. |
| 311 | .IP "2." 5 |
| 312 | The \fB\-w\fR flag just sets the global \f(CW$^W\fR variable as in 5.005 \*(-- this |
| 313 | means that any legacy code that currently relies on manipulating \f(CW$^W\fR |
| 314 | to control warning behavior will still work as is. |
| 315 | .IP "3." 5 |
| 316 | Apart from now being a boolean, the \f(CW$^W\fR variable operates in exactly |
| 317 | the same horrible uncontrolled global way, except that it cannot |
| 318 | disable/enable default warnings. |
| 319 | .IP "4." 5 |
| 320 | If a piece of code is under the control of the \f(CW\*(C`warnings\*(C'\fR pragma, |
| 321 | both the \f(CW$^W\fR variable and the \fB\-w\fR flag will be ignored for the |
| 322 | scope of the lexical warning. |
| 323 | .IP "5." 5 |
| 324 | The only way to override a lexical warnings setting is with the \fB\-W\fR |
| 325 | or \fB\-X\fR command line flags. |
| 326 | .PP |
| 327 | The combined effect of 3 & 4 is that it will allow code which uses |
| 328 | the \f(CW\*(C`warnings\*(C'\fR pragma to control the warning behavior of $^W\-type |
| 329 | code (using a \f(CW\*(C`local $^W=0\*(C'\fR) if it really wants to, but not vice\-versa. |
| 330 | .Sh "Category Hierarchy" |
| 331 | .IX Xref "warning, categories" |
| 332 | .IX Subsection "Category Hierarchy" |
| 333 | A hierarchy of \*(L"categories\*(R" have been defined to allow groups of warnings |
| 334 | to be enabled/disabled in isolation. |
| 335 | .PP |
| 336 | The current hierarchy is: |
| 337 | .PP |
| 338 | .Vb 93 |
| 339 | \& all -+ |
| 340 | \& | |
| 341 | \& +- closure |
| 342 | \& | |
| 343 | \& +- deprecated |
| 344 | \& | |
| 345 | \& +- exiting |
| 346 | \& | |
| 347 | \& +- glob |
| 348 | \& | |
| 349 | \& +- io -----------+ |
| 350 | \& | | |
| 351 | \& | +- closed |
| 352 | \& | | |
| 353 | \& | +- exec |
| 354 | \& | | |
| 355 | \& | +- layer |
| 356 | \& | | |
| 357 | \& | +- newline |
| 358 | \& | | |
| 359 | \& | +- pipe |
| 360 | \& | | |
| 361 | \& | +- unopened |
| 362 | \& | |
| 363 | \& +- misc |
| 364 | \& | |
| 365 | \& +- numeric |
| 366 | \& | |
| 367 | \& +- once |
| 368 | \& | |
| 369 | \& +- overflow |
| 370 | \& | |
| 371 | \& +- pack |
| 372 | \& | |
| 373 | \& +- portable |
| 374 | \& | |
| 375 | \& +- recursion |
| 376 | \& | |
| 377 | \& +- redefine |
| 378 | \& | |
| 379 | \& +- regexp |
| 380 | \& | |
| 381 | \& +- severe -------+ |
| 382 | \& | | |
| 383 | \& | +- debugging |
| 384 | \& | | |
| 385 | \& | +- inplace |
| 386 | \& | | |
| 387 | \& | +- internal |
| 388 | \& | | |
| 389 | \& | +- malloc |
| 390 | \& | |
| 391 | \& +- signal |
| 392 | \& | |
| 393 | \& +- substr |
| 394 | \& | |
| 395 | \& +- syntax -------+ |
| 396 | \& | | |
| 397 | \& | +- ambiguous |
| 398 | \& | | |
| 399 | \& | +- bareword |
| 400 | \& | | |
| 401 | \& | +- digit |
| 402 | \& | | |
| 403 | \& | +- parenthesis |
| 404 | \& | | |
| 405 | \& | +- precedence |
| 406 | \& | | |
| 407 | \& | +- printf |
| 408 | \& | | |
| 409 | \& | +- prototype |
| 410 | \& | | |
| 411 | \& | +- qw |
| 412 | \& | | |
| 413 | \& | +- reserved |
| 414 | \& | | |
| 415 | \& | +- semicolon |
| 416 | \& | |
| 417 | \& +- taint |
| 418 | \& | |
| 419 | \& +- threads |
| 420 | \& | |
| 421 | \& +- uninitialized |
| 422 | \& | |
| 423 | \& +- unpack |
| 424 | \& | |
| 425 | \& +- untie |
| 426 | \& | |
| 427 | \& +- utf8 |
| 428 | \& | |
| 429 | \& +- void |
| 430 | \& | |
| 431 | \& +- y2k |
| 432 | .Ve |
| 433 | .PP |
| 434 | Just like the \*(L"strict\*(R" pragma any of these categories can be combined |
| 435 | .PP |
| 436 | .Vb 2 |
| 437 | \& use warnings qw(void redefine); |
| 438 | \& no warnings qw(io syntax untie); |
| 439 | .Ve |
| 440 | .PP |
| 441 | Also like the \*(L"strict\*(R" pragma, if there is more than one instance of the |
| 442 | \&\f(CW\*(C`warnings\*(C'\fR pragma in a given scope the cumulative effect is additive. |
| 443 | .PP |
| 444 | .Vb 5 |
| 445 | \& use warnings qw(void); # only "void" warnings enabled |
| 446 | \& ... |
| 447 | \& use warnings qw(io); # only "void" & "io" warnings enabled |
| 448 | \& ... |
| 449 | \& no warnings qw(void); # only "io" warnings enabled |
| 450 | .Ve |
| 451 | .PP |
| 452 | To determine which category a specific warning has been assigned to see |
| 453 | perldiag. |
| 454 | .PP |
| 455 | Note: In Perl 5.6.1, the lexical warnings category \*(L"deprecated\*(R" was a |
| 456 | sub-category of the \*(L"syntax\*(R" category. It is now a top-level category |
| 457 | in its own right. |
| 458 | .Sh "Fatal Warnings" |
| 459 | .IX Xref "warning, fatal" |
| 460 | .IX Subsection "Fatal Warnings" |
| 461 | The presence of the word \*(L"\s-1FATAL\s0\*(R" in the category list will escalate any |
| 462 | warnings detected from the categories specified in the lexical scope |
| 463 | into fatal errors. In the code below, the use of \f(CW\*(C`time\*(C'\fR, \f(CW\*(C`length\*(C'\fR |
| 464 | and \f(CW\*(C`join\*(C'\fR can all produce a \f(CW"Useless use of xxx in void context"\fR |
| 465 | warning. |
| 466 | .PP |
| 467 | .Vb 1 |
| 468 | \& use warnings; |
| 469 | .Ve |
| 470 | .PP |
| 471 | .Vb 1 |
| 472 | \& time; |
| 473 | .Ve |
| 474 | .PP |
| 475 | .Vb 4 |
| 476 | \& { |
| 477 | \& use warnings FATAL => qw(void); |
| 478 | \& length "abc"; |
| 479 | \& } |
| 480 | .Ve |
| 481 | .PP |
| 482 | .Vb 1 |
| 483 | \& join "", 1,2,3; |
| 484 | .Ve |
| 485 | .PP |
| 486 | .Vb 1 |
| 487 | \& print "done\en"; |
| 488 | .Ve |
| 489 | .PP |
| 490 | When run it produces this output |
| 491 | .PP |
| 492 | .Vb 2 |
| 493 | \& Useless use of time in void context at fatal line 3. |
| 494 | \& Useless use of length in void context at fatal line 7. |
| 495 | .Ve |
| 496 | .PP |
| 497 | The scope where \f(CW\*(C`length\*(C'\fR is used has escalated the \f(CW\*(C`void\*(C'\fR warnings |
| 498 | category into a fatal error, so the program terminates immediately it |
| 499 | encounters the warning. |
| 500 | .PP |
| 501 | To explicitly turn off a \*(L"\s-1FATAL\s0\*(R" warning you just disable the warning |
| 502 | it is associated with. So, for example, to disable the \*(L"void\*(R" warning |
| 503 | in the example above, either of these will do the trick: |
| 504 | .PP |
| 505 | .Vb 2 |
| 506 | \& no warnings qw(void); |
| 507 | \& no warnings FATAL => qw(void); |
| 508 | .Ve |
| 509 | .PP |
| 510 | If you want to downgrade a warning that has been escalated into a fatal |
| 511 | error back to a normal warning, you can use the \*(L"\s-1NONFATAL\s0\*(R" keyword. For |
| 512 | example, the code below will promote all warnings into fatal errors, |
| 513 | except for those in the \*(L"syntax\*(R" category. |
| 514 | .PP |
| 515 | .Vb 1 |
| 516 | \& use warnings FATAL => 'all', NONFATAL => 'syntax'; |
| 517 | .Ve |
| 518 | .Sh "Reporting Warnings from a Module" |
| 519 | .IX Xref "warning, reporting warning, registering" |
| 520 | .IX Subsection "Reporting Warnings from a Module" |
| 521 | The \f(CW\*(C`warnings\*(C'\fR pragma provides a number of functions that are useful for |
| 522 | module authors. These are used when you want to report a module-specific |
| 523 | warning to a calling module has enabled warnings via the \f(CW\*(C`warnings\*(C'\fR |
| 524 | pragma. |
| 525 | .PP |
| 526 | Consider the module \f(CW\*(C`MyMod::Abc\*(C'\fR below. |
| 527 | .PP |
| 528 | .Vb 1 |
| 529 | \& package MyMod::Abc; |
| 530 | .Ve |
| 531 | .PP |
| 532 | .Vb 1 |
| 533 | \& use warnings::register; |
| 534 | .Ve |
| 535 | .PP |
| 536 | .Vb 8 |
| 537 | \& sub open { |
| 538 | \& my $path = shift; |
| 539 | \& if ($path !~ m#^/#) { |
| 540 | \& warnings::warn("changing relative path to /var/abc") |
| 541 | \& if warnings::enabled(); |
| 542 | \& $path = "/var/abc/$path"; |
| 543 | \& } |
| 544 | \& } |
| 545 | .Ve |
| 546 | .PP |
| 547 | .Vb 1 |
| 548 | \& 1; |
| 549 | .Ve |
| 550 | .PP |
| 551 | The call to \f(CW\*(C`warnings::register\*(C'\fR will create a new warnings category |
| 552 | called \*(L"MyMod::abc\*(R", i.e. the new category name matches the current |
| 553 | package name. The \f(CW\*(C`open\*(C'\fR function in the module will display a warning |
| 554 | message if it gets given a relative path as a parameter. This warnings |
| 555 | will only be displayed if the code that uses \f(CW\*(C`MyMod::Abc\*(C'\fR has actually |
| 556 | enabled them with the \f(CW\*(C`warnings\*(C'\fR pragma like below. |
| 557 | .PP |
| 558 | .Vb 4 |
| 559 | \& use MyMod::Abc; |
| 560 | \& use warnings 'MyMod::Abc'; |
| 561 | \& ... |
| 562 | \& abc::open("../fred.txt"); |
| 563 | .Ve |
| 564 | .PP |
| 565 | It is also possible to test whether the pre-defined warnings categories are |
| 566 | set in the calling module with the \f(CW\*(C`warnings::enabled\*(C'\fR function. Consider |
| 567 | this snippet of code: |
| 568 | .PP |
| 569 | .Vb 1 |
| 570 | \& package MyMod::Abc; |
| 571 | .Ve |
| 572 | .PP |
| 573 | .Vb 5 |
| 574 | \& sub open { |
| 575 | \& warnings::warnif("deprecated", |
| 576 | \& "open is deprecated, use new instead"); |
| 577 | \& new(@_); |
| 578 | \& } |
| 579 | .Ve |
| 580 | .PP |
| 581 | .Vb 3 |
| 582 | \& sub new |
| 583 | \& ... |
| 584 | \& 1; |
| 585 | .Ve |
| 586 | .PP |
| 587 | The function \f(CW\*(C`open\*(C'\fR has been deprecated, so code has been included to |
| 588 | display a warning message whenever the calling module has (at least) the |
| 589 | \&\*(L"deprecated\*(R" warnings category enabled. Something like this, say. |
| 590 | .PP |
| 591 | .Vb 4 |
| 592 | \& use warnings 'deprecated'; |
| 593 | \& use MyMod::Abc; |
| 594 | \& ... |
| 595 | \& MyMod::Abc::open($filename); |
| 596 | .Ve |
| 597 | .PP |
| 598 | Either the \f(CW\*(C`warnings::warn\*(C'\fR or \f(CW\*(C`warnings::warnif\*(C'\fR function should be |
| 599 | used to actually display the warnings message. This is because they can |
| 600 | make use of the feature that allows warnings to be escalated into fatal |
| 601 | errors. So in this case |
| 602 | .PP |
| 603 | .Vb 4 |
| 604 | \& use MyMod::Abc; |
| 605 | \& use warnings FATAL => 'MyMod::Abc'; |
| 606 | \& ... |
| 607 | \& MyMod::Abc::open('../fred.txt'); |
| 608 | .Ve |
| 609 | .PP |
| 610 | the \f(CW\*(C`warnings::warnif\*(C'\fR function will detect this and die after |
| 611 | displaying the warning message. |
| 612 | .PP |
| 613 | The three warnings functions, \f(CW\*(C`warnings::warn\*(C'\fR, \f(CW\*(C`warnings::warnif\*(C'\fR |
| 614 | and \f(CW\*(C`warnings::enabled\*(C'\fR can optionally take an object reference in place |
| 615 | of a category name. In this case the functions will use the class name |
| 616 | of the object as the warnings category. |
| 617 | .PP |
| 618 | Consider this example: |
| 619 | .PP |
| 620 | .Vb 1 |
| 621 | \& package Original; |
| 622 | .Ve |
| 623 | .PP |
| 624 | .Vb 2 |
| 625 | \& no warnings; |
| 626 | \& use warnings::register; |
| 627 | .Ve |
| 628 | .PP |
| 629 | .Vb 5 |
| 630 | \& sub new |
| 631 | \& { |
| 632 | \& my $class = shift; |
| 633 | \& bless [], $class; |
| 634 | \& } |
| 635 | .Ve |
| 636 | .PP |
| 637 | .Vb 4 |
| 638 | \& sub check |
| 639 | \& { |
| 640 | \& my $self = shift; |
| 641 | \& my $value = shift; |
| 642 | .Ve |
| 643 | .PP |
| 644 | .Vb 3 |
| 645 | \& if ($value % 2 && warnings::enabled($self)) |
| 646 | \& { warnings::warn($self, "Odd numbers are unsafe") } |
| 647 | \& } |
| 648 | .Ve |
| 649 | .PP |
| 650 | .Vb 7 |
| 651 | \& sub doit |
| 652 | \& { |
| 653 | \& my $self = shift; |
| 654 | \& my $value = shift; |
| 655 | \& $self->check($value); |
| 656 | \& # ... |
| 657 | \& } |
| 658 | .Ve |
| 659 | .PP |
| 660 | .Vb 1 |
| 661 | \& 1; |
| 662 | .Ve |
| 663 | .PP |
| 664 | .Vb 1 |
| 665 | \& package Derived; |
| 666 | .Ve |
| 667 | .PP |
| 668 | .Vb 8 |
| 669 | \& use warnings::register; |
| 670 | \& use Original; |
| 671 | \& our @ISA = qw( Original ); |
| 672 | \& sub new |
| 673 | \& { |
| 674 | \& my $class = shift; |
| 675 | \& bless [], $class; |
| 676 | \& } |
| 677 | .Ve |
| 678 | .PP |
| 679 | .Vb 1 |
| 680 | \& 1; |
| 681 | .Ve |
| 682 | .PP |
| 683 | The code below makes use of both modules, but it only enables warnings from |
| 684 | \&\f(CW\*(C`Derived\*(C'\fR. |
| 685 | .PP |
| 686 | .Vb 7 |
| 687 | \& use Original; |
| 688 | \& use Derived; |
| 689 | \& use warnings 'Derived'; |
| 690 | \& my $a = new Original; |
| 691 | \& $a->doit(1); |
| 692 | \& my $b = new Derived; |
| 693 | \& $a->doit(1); |
| 694 | .Ve |
| 695 | .PP |
| 696 | When this code is run only the \f(CW\*(C`Derived\*(C'\fR object, \f(CW$b\fR, will generate |
| 697 | a warning. |
| 698 | .PP |
| 699 | .Vb 1 |
| 700 | \& Odd numbers are unsafe at main.pl line 7 |
| 701 | .Ve |
| 702 | .PP |
| 703 | Notice also that the warning is reported at the line where the object is first |
| 704 | used. |
| 705 | .SH "TODO" |
| 706 | .IX Header "TODO" |
| 707 | .Vb 4 |
| 708 | \& perl5db.pl |
| 709 | \& The debugger saves and restores C<$^W> at runtime. I haven't checked |
| 710 | \& whether the debugger will still work with the lexical warnings |
| 711 | \& patch applied. |
| 712 | .Ve |
| 713 | .PP |
| 714 | .Vb 5 |
| 715 | \& diagnostics.pm |
| 716 | \& I *think* I've got diagnostics to work with the lexical warnings |
| 717 | \& patch, but there were design decisions made in diagnostics to work |
| 718 | \& around the limitations of C<$^W>. Now that those limitations are gone, |
| 719 | \& the module should be revisited. |
| 720 | .Ve |
| 721 | .PP |
| 722 | .Vb 1 |
| 723 | \& document calling the warnings::* functions from XS |
| 724 | .Ve |
| 725 | .SH "SEE ALSO" |
| 726 | .IX Header "SEE ALSO" |
| 727 | warnings, perldiag. |
| 728 | .SH "AUTHOR" |
| 729 | .IX Header "AUTHOR" |
| 730 | Paul Marquess |