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