| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # Regenerate (overwriting only if changed): |
| 4 | # |
| 5 | # lib/warnings.pm |
| 6 | # warnings.h |
| 7 | # |
| 8 | # from information hardcoded into this script (the $tree hash), plus the |
| 9 | # template for warnings.pm in the DATA section. |
| 10 | # |
| 11 | # When changing the number of warnings, t/op/caller.t should change to |
| 12 | # correspond with the value of $BYTES in lib/warnings.pm |
| 13 | # |
| 14 | # With an argument of 'tree', just dump the contents of $tree and exits. |
| 15 | # Also accepts the standard regen_lib -q and -v args. |
| 16 | # |
| 17 | # This script is normally invoked from regen.pl. |
| 18 | |
| 19 | $VERSION = '1.03'; |
| 20 | |
| 21 | BEGIN { |
| 22 | require 'regen/regen_lib.pl'; |
| 23 | push @INC, './lib'; |
| 24 | } |
| 25 | use strict ; |
| 26 | |
| 27 | sub DEFAULT_ON () { 1 } |
| 28 | sub DEFAULT_OFF () { 2 } |
| 29 | |
| 30 | my $tree = { |
| 31 | 'everything' => [ 5.021, { |
| 32 | 'all' => [ 5.008, { |
| 33 | 'io' => [ 5.008, { |
| 34 | 'pipe' => [ 5.008, DEFAULT_OFF], |
| 35 | 'unopened' => [ 5.008, DEFAULT_OFF], |
| 36 | 'closed' => [ 5.008, DEFAULT_OFF], |
| 37 | 'newline' => [ 5.008, DEFAULT_OFF], |
| 38 | 'exec' => [ 5.008, DEFAULT_OFF], |
| 39 | 'layer' => [ 5.008, DEFAULT_OFF], |
| 40 | 'syscalls' => [ 5.019, DEFAULT_OFF], |
| 41 | }], |
| 42 | 'syntax' => [ 5.008, { |
| 43 | 'ambiguous' => [ 5.008, DEFAULT_OFF], |
| 44 | 'semicolon' => [ 5.008, DEFAULT_OFF], |
| 45 | 'precedence' => [ 5.008, DEFAULT_OFF], |
| 46 | 'bareword' => [ 5.008, DEFAULT_OFF], |
| 47 | 'reserved' => [ 5.008, DEFAULT_OFF], |
| 48 | 'digit' => [ 5.008, DEFAULT_OFF], |
| 49 | 'parenthesis' => [ 5.008, DEFAULT_OFF], |
| 50 | 'printf' => [ 5.008, DEFAULT_OFF], |
| 51 | 'prototype' => [ 5.008, DEFAULT_OFF], |
| 52 | 'qw' => [ 5.008, DEFAULT_OFF], |
| 53 | 'illegalproto' => [ 5.011, DEFAULT_OFF], |
| 54 | }], |
| 55 | 'severe' => [ 5.008, { |
| 56 | 'inplace' => [ 5.008, DEFAULT_ON], |
| 57 | 'internal' => [ 5.008, DEFAULT_OFF], |
| 58 | 'debugging' => [ 5.008, DEFAULT_ON], |
| 59 | 'malloc' => [ 5.008, DEFAULT_ON], |
| 60 | }], |
| 61 | 'deprecated' => [ 5.008, DEFAULT_ON], |
| 62 | 'void' => [ 5.008, DEFAULT_OFF], |
| 63 | 'recursion' => [ 5.008, DEFAULT_OFF], |
| 64 | 'redefine' => [ 5.008, DEFAULT_OFF], |
| 65 | 'numeric' => [ 5.008, DEFAULT_OFF], |
| 66 | 'uninitialized' => [ 5.008, DEFAULT_OFF], |
| 67 | 'once' => [ 5.008, DEFAULT_OFF], |
| 68 | 'misc' => [ 5.008, DEFAULT_OFF], |
| 69 | 'regexp' => [ 5.008, DEFAULT_OFF], |
| 70 | 'glob' => [ 5.008, DEFAULT_ON], |
| 71 | 'untie' => [ 5.008, DEFAULT_OFF], |
| 72 | 'substr' => [ 5.008, DEFAULT_OFF], |
| 73 | 'taint' => [ 5.008, DEFAULT_OFF], |
| 74 | 'signal' => [ 5.008, DEFAULT_OFF], |
| 75 | 'closure' => [ 5.008, DEFAULT_OFF], |
| 76 | 'overflow' => [ 5.008, DEFAULT_OFF], |
| 77 | 'portable' => [ 5.008, DEFAULT_OFF], |
| 78 | 'utf8' => [ 5.008, { |
| 79 | 'surrogate' => [ 5.013, DEFAULT_OFF], |
| 80 | 'nonchar' => [ 5.013, DEFAULT_OFF], |
| 81 | 'non_unicode' => [ 5.013, DEFAULT_OFF], |
| 82 | }], |
| 83 | 'exiting' => [ 5.008, DEFAULT_OFF], |
| 84 | 'pack' => [ 5.008, DEFAULT_OFF], |
| 85 | 'unpack' => [ 5.008, DEFAULT_OFF], |
| 86 | 'threads' => [ 5.008, DEFAULT_OFF], |
| 87 | 'imprecision' => [ 5.011, DEFAULT_OFF], |
| 88 | 'experimental' => [ 5.017, { |
| 89 | 'experimental::lexical_subs' => |
| 90 | [ 5.017, DEFAULT_ON ], |
| 91 | 'experimental::regex_sets' => |
| 92 | [ 5.017, DEFAULT_ON ], |
| 93 | 'experimental::lexical_topic' => |
| 94 | [ 5.017, DEFAULT_ON ], |
| 95 | 'experimental::smartmatch' => |
| 96 | [ 5.017, DEFAULT_ON ], |
| 97 | 'experimental::postderef' => |
| 98 | [ 5.019, DEFAULT_ON ], |
| 99 | 'experimental::autoderef' => |
| 100 | [ 5.019, DEFAULT_ON ], |
| 101 | 'experimental::signatures' => |
| 102 | [ 5.019, DEFAULT_ON ], |
| 103 | 'experimental::win32_perlio' => |
| 104 | [ 5.021, DEFAULT_ON ], |
| 105 | 'experimental::refaliasing' => |
| 106 | [ 5.021, DEFAULT_ON ], |
| 107 | 'experimental::re_strict' => |
| 108 | [ 5.021, DEFAULT_ON ], |
| 109 | }], |
| 110 | |
| 111 | 'missing' => [ 5.021, DEFAULT_OFF], |
| 112 | 'redundant' => [ 5.021, DEFAULT_OFF], |
| 113 | 'locale' => [ 5.021, DEFAULT_ON], |
| 114 | |
| 115 | #'default' => [ 5.008, DEFAULT_ON ], |
| 116 | }], |
| 117 | |
| 118 | # When adding new warnings outside of "all" make sure to |
| 119 | # either patch util.c to ignore them under -w and -W, or to |
| 120 | # generalize the facility for adding them so that it knows |
| 121 | # about warnings outside of "all". |
| 122 | 'extra' => [ 5.021, { |
| 123 | 'void_unusual' => [ 5.021, DEFAULT_OFF], |
| 124 | }], |
| 125 | }]}; |
| 126 | |
| 127 | my @def ; |
| 128 | my %list ; |
| 129 | my %Value ; |
| 130 | my %ValueToName ; |
| 131 | my %NameToValue ; |
| 132 | |
| 133 | my %v_list = () ; |
| 134 | |
| 135 | sub valueWalk |
| 136 | { |
| 137 | my $tre = shift ; |
| 138 | my @list = () ; |
| 139 | my ($k, $v) ; |
| 140 | |
| 141 | foreach $k (sort keys %$tre) { |
| 142 | $v = $tre->{$k}; |
| 143 | die "duplicate key $k\n" if defined $list{$k} ; |
| 144 | die "Value associated with key '$k' is not an ARRAY reference" |
| 145 | if !ref $v || ref $v ne 'ARRAY' ; |
| 146 | |
| 147 | my ($ver, $rest) = @{ $v } ; |
| 148 | push @{ $v_list{$ver} }, $k; |
| 149 | |
| 150 | if (ref $rest) |
| 151 | { valueWalk ($rest) } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | sub orderValues |
| 158 | { |
| 159 | my $index = 0; |
| 160 | foreach my $ver ( sort { $a <=> $b } keys %v_list ) { |
| 161 | foreach my $name (@{ $v_list{$ver} } ) { |
| 162 | $ValueToName{ $index } = [ uc $name, $ver ] ; |
| 163 | $NameToValue{ uc $name } = $index ++ ; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return $index ; |
| 168 | } |
| 169 | |
| 170 | ########################################################################### |
| 171 | |
| 172 | sub walk |
| 173 | { |
| 174 | my $tre = shift ; |
| 175 | my @list = () ; |
| 176 | my ($k, $v) ; |
| 177 | |
| 178 | foreach $k (sort keys %$tre) { |
| 179 | $v = $tre->{$k}; |
| 180 | die "duplicate key $k\n" if defined $list{$k} ; |
| 181 | die "Can't find key '$k'" |
| 182 | if ! defined $NameToValue{uc $k} ; |
| 183 | push @{ $list{$k} }, $NameToValue{uc $k} ; |
| 184 | die "Value associated with key '$k' is not an ARRAY reference" |
| 185 | if !ref $v || ref $v ne 'ARRAY' ; |
| 186 | |
| 187 | my ($ver, $rest) = @{ $v } ; |
| 188 | if (ref $rest) |
| 189 | { push (@{ $list{$k} }, walk ($rest)) } |
| 190 | elsif ($rest == DEFAULT_ON) |
| 191 | { push @def, $NameToValue{uc $k} } |
| 192 | |
| 193 | push @list, @{ $list{$k} } ; |
| 194 | } |
| 195 | |
| 196 | return @list ; |
| 197 | } |
| 198 | |
| 199 | ########################################################################### |
| 200 | |
| 201 | sub mkRange |
| 202 | { |
| 203 | my @a = @_ ; |
| 204 | my @out = @a ; |
| 205 | |
| 206 | for my $i (1 .. @a - 1) { |
| 207 | $out[$i] = ".." |
| 208 | if $a[$i] == $a[$i - 1] + 1 |
| 209 | && ($i >= @a - 1 || $a[$i] + 1 == $a[$i + 1] ); |
| 210 | } |
| 211 | $out[-1] = $a[-1] if $out[-1] eq ".."; |
| 212 | |
| 213 | my $out = join(",",@out); |
| 214 | |
| 215 | $out =~ s/,(\.\.,)+/../g ; |
| 216 | return $out; |
| 217 | } |
| 218 | |
| 219 | ########################################################################### |
| 220 | sub warningsTree |
| 221 | { |
| 222 | my $tre = shift ; |
| 223 | my $prefix = shift ; |
| 224 | my ($k, $v) ; |
| 225 | |
| 226 | my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ; |
| 227 | my @keys = sort keys %$tre ; |
| 228 | |
| 229 | my $rv = ''; |
| 230 | |
| 231 | while ($k = shift @keys) { |
| 232 | $v = $tre->{$k}; |
| 233 | die "Value associated with key '$k' is not an ARRAY reference" |
| 234 | if !ref $v || ref $v ne 'ARRAY' ; |
| 235 | |
| 236 | my $offset ; |
| 237 | if ($tre ne $tree) { |
| 238 | $rv .= $prefix . "|\n" ; |
| 239 | $rv .= $prefix . "+- $k" ; |
| 240 | $offset = ' ' x ($max + 4) ; |
| 241 | } |
| 242 | else { |
| 243 | $rv .= $prefix . "$k" ; |
| 244 | $offset = ' ' x ($max + 1) ; |
| 245 | } |
| 246 | |
| 247 | my ($ver, $rest) = @{ $v } ; |
| 248 | if (ref $rest) |
| 249 | { |
| 250 | my $bar = @keys ? "|" : " "; |
| 251 | $rv .= " -" . "-" x ($max - length $k ) . "+\n" ; |
| 252 | $rv .= warningsTree ($rest, $prefix . $bar . $offset ) |
| 253 | } |
| 254 | else |
| 255 | { $rv .= "\n" } |
| 256 | } |
| 257 | |
| 258 | return $rv; |
| 259 | } |
| 260 | |
| 261 | ########################################################################### |
| 262 | |
| 263 | sub mkHexOct |
| 264 | { |
| 265 | my ($f, $max, @a) = @_ ; |
| 266 | my $mask = "\x00" x $max ; |
| 267 | my $string = "" ; |
| 268 | |
| 269 | foreach (@a) { |
| 270 | vec($mask, $_, 1) = 1 ; |
| 271 | } |
| 272 | |
| 273 | foreach (unpack("C*", $mask)) { |
| 274 | if ($f eq 'x') { |
| 275 | $string .= '\x' . sprintf("%2.2x", $_) |
| 276 | } |
| 277 | else { |
| 278 | $string .= '\\' . sprintf("%o", $_) |
| 279 | } |
| 280 | } |
| 281 | return $string ; |
| 282 | } |
| 283 | |
| 284 | sub mkHex |
| 285 | { |
| 286 | my($max, @a) = @_; |
| 287 | return mkHexOct("x", $max, @a); |
| 288 | } |
| 289 | |
| 290 | sub mkOct |
| 291 | { |
| 292 | my($max, @a) = @_; |
| 293 | return mkHexOct("o", $max, @a); |
| 294 | } |
| 295 | |
| 296 | ########################################################################### |
| 297 | |
| 298 | if (@ARGV && $ARGV[0] eq "tree") |
| 299 | { |
| 300 | print warningsTree($tree, " ") ; |
| 301 | exit ; |
| 302 | } |
| 303 | |
| 304 | my ($warn, $pm) = map { |
| 305 | open_new($_, '>', { by => 'regen/warnings.pl' }); |
| 306 | } 'warnings.h', 'lib/warnings.pm'; |
| 307 | |
| 308 | my ($index, $warn_size); |
| 309 | |
| 310 | { |
| 311 | # generate warnings.h |
| 312 | |
| 313 | print $warn <<'EOM'; |
| 314 | |
| 315 | #define Off(x) ((x) / 8) |
| 316 | #define Bit(x) (1 << ((x) % 8)) |
| 317 | #define IsSet(a, x) ((a)[Off(x)] & Bit(x)) |
| 318 | |
| 319 | |
| 320 | #define G_WARN_OFF 0 /* $^W == 0 */ |
| 321 | #define G_WARN_ON 1 /* -w flag and $^W != 0 */ |
| 322 | #define G_WARN_ALL_ON 2 /* -W flag */ |
| 323 | #define G_WARN_ALL_OFF 4 /* -X flag */ |
| 324 | #define G_WARN_ONCE 8 /* set if 'once' ever enabled */ |
| 325 | #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF) |
| 326 | |
| 327 | #define pWARN_STD NULL |
| 328 | #define pWARN_ALL (((STRLEN*)0)+1) /* use warnings 'all' */ |
| 329 | #define pWARN_NONE (((STRLEN*)0)+2) /* no warnings 'all' */ |
| 330 | |
| 331 | #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \ |
| 332 | (x) == pWARN_NONE) |
| 333 | |
| 334 | /* if PL_warnhook is set to this value, then warnings die */ |
| 335 | #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder) |
| 336 | EOM |
| 337 | |
| 338 | my $offset = 0 ; |
| 339 | |
| 340 | valueWalk ($tree) ; |
| 341 | $index = orderValues(); |
| 342 | |
| 343 | die <<EOM if $index > 255 ; |
| 344 | Too many warnings categories -- max is 255 |
| 345 | rewrite packWARN* & unpackWARN* macros |
| 346 | EOM |
| 347 | |
| 348 | walk ($tree) ; |
| 349 | |
| 350 | $index *= 2 ; |
| 351 | $warn_size = int($index / 8) + ($index % 8 != 0) ; |
| 352 | |
| 353 | my $k ; |
| 354 | my $last_ver = 0; |
| 355 | foreach $k (sort { $a <=> $b } keys %ValueToName) { |
| 356 | my ($name, $version) = @{ $ValueToName{$k} }; |
| 357 | print $warn "\n/* Warnings Categories added in Perl $version */\n\n" |
| 358 | if $last_ver != $version ; |
| 359 | $name =~ y/:/_/; |
| 360 | print $warn tab(5, "#define WARN_$name"), " $k\n" ; |
| 361 | $last_ver = $version ; |
| 362 | } |
| 363 | print $warn "\n" ; |
| 364 | |
| 365 | print $warn tab(5, '#define WARNsize'), "$warn_size\n" ; |
| 366 | print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ; |
| 367 | print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ; |
| 368 | |
| 369 | print $warn <<'EOM'; |
| 370 | |
| 371 | #define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD) |
| 372 | #define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD) |
| 373 | #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE)) |
| 374 | #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x))) |
| 375 | #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1)) |
| 376 | |
| 377 | #define DUP_WARNINGS(p) \ |
| 378 | (specialWARN(p) ? (STRLEN*)(p) \ |
| 379 | : (STRLEN*)CopyD(p, PerlMemShared_malloc(sizeof(*p)+*p), sizeof(*p)+*p, \ |
| 380 | char)) |
| 381 | |
| 382 | #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w)) |
| 383 | |
| 384 | /* The w1, w2 ... should be independent warnings categories; one shouldn't be |
| 385 | * a subcategory of any other */ |
| 386 | |
| 387 | #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2)) |
| 388 | #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3)) |
| 389 | #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4)) |
| 390 | |
| 391 | #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w)) |
| 392 | #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2)) |
| 393 | #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3)) |
| 394 | #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4)) |
| 395 | |
| 396 | #define WARNshift 8 |
| 397 | |
| 398 | #define packWARN(a) (a ) |
| 399 | |
| 400 | /* The a, b, ... should be independent warnings categories; one shouldn't be |
| 401 | * a subcategory of any other */ |
| 402 | |
| 403 | #define packWARN2(a,b) ((a) | ((b)<<8) ) |
| 404 | #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) ) |
| 405 | #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24)) |
| 406 | |
| 407 | #define unpackWARN1(x) ((x) & 0xFF) |
| 408 | #define unpackWARN2(x) (((x) >>8) & 0xFF) |
| 409 | #define unpackWARN3(x) (((x) >>16) & 0xFF) |
| 410 | #define unpackWARN4(x) (((x) >>24) & 0xFF) |
| 411 | |
| 412 | #define ckDEAD(x) \ |
| 413 | ( ! specialWARN(PL_curcop->cop_warnings) && \ |
| 414 | ( isWARNf_on(PL_curcop->cop_warnings, WARN_ALL) || \ |
| 415 | isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \ |
| 416 | isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \ |
| 417 | isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \ |
| 418 | isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x)))) |
| 419 | |
| 420 | /* end of file warnings.h */ |
| 421 | EOM |
| 422 | |
| 423 | read_only_bottom_close_and_rename($warn); |
| 424 | } |
| 425 | |
| 426 | while (<DATA>) { |
| 427 | last if /^KEYWORDS$/ ; |
| 428 | print $pm $_ ; |
| 429 | } |
| 430 | |
| 431 | my $last_ver = 0; |
| 432 | print $pm "our %Offsets = (\n" ; |
| 433 | foreach my $k (sort { $a <=> $b } keys %ValueToName) { |
| 434 | my ($name, $version) = @{ $ValueToName{$k} }; |
| 435 | $name = lc $name; |
| 436 | $k *= 2 ; |
| 437 | if ( $last_ver != $version ) { |
| 438 | print $pm "\n"; |
| 439 | print $pm tab(4, " # Warnings Categories added in Perl $version"); |
| 440 | print $pm "\n\n"; |
| 441 | } |
| 442 | print $pm tab(4, " '$name'"), "=> $k,\n" ; |
| 443 | $last_ver = $version; |
| 444 | } |
| 445 | |
| 446 | print $pm " );\n\n" ; |
| 447 | |
| 448 | print $pm "our %Bits = (\n" ; |
| 449 | foreach my $k (sort keys %list) { |
| 450 | |
| 451 | my $v = $list{$k} ; |
| 452 | my @list = sort { $a <=> $b } @$v ; |
| 453 | |
| 454 | print $pm tab(4, " '$k'"), '=> "', |
| 455 | mkHex($warn_size, map $_ * 2 , @list), |
| 456 | '", # [', mkRange(@list), "]\n" ; |
| 457 | } |
| 458 | |
| 459 | print $pm " );\n\n" ; |
| 460 | |
| 461 | print $pm "our %DeadBits = (\n" ; |
| 462 | foreach my $k (sort keys %list) { |
| 463 | |
| 464 | my $v = $list{$k} ; |
| 465 | my @list = sort { $a <=> $b } @$v ; |
| 466 | |
| 467 | print $pm tab(4, " '$k'"), '=> "', |
| 468 | mkHex($warn_size, map $_ * 2 + 1 , @list), |
| 469 | '", # [', mkRange(@list), "]\n" ; |
| 470 | } |
| 471 | |
| 472 | print $pm " );\n\n" ; |
| 473 | print $pm '$NONE = "', ('\0' x $warn_size) , "\";\n" ; |
| 474 | print $pm '$DEFAULT = "', mkHex($warn_size, map $_ * 2, @def), |
| 475 | '", # [', mkRange(@def), "]\n" ; |
| 476 | print $pm '$LAST_BIT = ' . "$index ;\n" ; |
| 477 | print $pm '$BYTES = ' . "$warn_size ;\n" ; |
| 478 | while (<DATA>) { |
| 479 | if ($_ eq "=for warnings.pl tree-goes-here\n") { |
| 480 | print $pm warningsTree($tree, " "); |
| 481 | next; |
| 482 | } |
| 483 | print $pm $_ ; |
| 484 | } |
| 485 | |
| 486 | read_only_bottom_close_and_rename($pm); |
| 487 | |
| 488 | __END__ |
| 489 | package warnings; |
| 490 | |
| 491 | our $VERSION = '1.30'; |
| 492 | |
| 493 | # Verify that we're called correctly so that warnings will work. |
| 494 | # see also strict.pm. |
| 495 | unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) { |
| 496 | my (undef, $f, $l) = caller; |
| 497 | die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n"); |
| 498 | } |
| 499 | |
| 500 | KEYWORDS |
| 501 | |
| 502 | $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ; |
| 503 | |
| 504 | sub Croaker |
| 505 | { |
| 506 | require Carp; # this initializes %CarpInternal |
| 507 | local $Carp::CarpInternal{'warnings'}; |
| 508 | delete $Carp::CarpInternal{'warnings'}; |
| 509 | Carp::croak(@_); |
| 510 | } |
| 511 | |
| 512 | sub _bits { |
| 513 | my $mask = shift ; |
| 514 | my $catmask ; |
| 515 | my $fatal = 0 ; |
| 516 | my $no_fatal = 0 ; |
| 517 | |
| 518 | foreach my $word ( @_ ) { |
| 519 | if ($word eq 'FATAL') { |
| 520 | $fatal = 1; |
| 521 | $no_fatal = 0; |
| 522 | } |
| 523 | elsif ($word eq 'NONFATAL') { |
| 524 | $fatal = 0; |
| 525 | $no_fatal = 1; |
| 526 | } |
| 527 | elsif ($catmask = $Bits{$word}) { |
| 528 | $mask |= $catmask ; |
| 529 | $mask |= $DeadBits{$word} if $fatal ; |
| 530 | $mask &= ~($DeadBits{$word}|$All) if $no_fatal ; |
| 531 | } |
| 532 | else |
| 533 | { Croaker("Unknown warnings category '$word'")} |
| 534 | } |
| 535 | |
| 536 | return $mask ; |
| 537 | } |
| 538 | |
| 539 | sub bits |
| 540 | { |
| 541 | # called from B::Deparse.pm |
| 542 | push @_, 'all' unless @_ ; |
| 543 | return _bits(undef, @_) ; |
| 544 | } |
| 545 | |
| 546 | sub import |
| 547 | { |
| 548 | shift; |
| 549 | |
| 550 | my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; |
| 551 | |
| 552 | if (vec($mask, $Offsets{'all'}, 1)) { |
| 553 | $mask |= $Bits{'all'} ; |
| 554 | $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1); |
| 555 | } |
| 556 | |
| 557 | # append 'all' when implied (after a lone "FATAL" or "NONFATAL") |
| 558 | push @_, 'all' if @_==1 && ( $_[0] eq 'FATAL' || $_[0] eq 'NONFATAL' ); |
| 559 | |
| 560 | # Empty @_ is equivalent to @_ = 'all' ; |
| 561 | ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ; |
| 562 | } |
| 563 | |
| 564 | sub unimport |
| 565 | { |
| 566 | shift; |
| 567 | |
| 568 | my $catmask ; |
| 569 | my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; |
| 570 | |
| 571 | if (vec($mask, $Offsets{'all'}, 1)) { |
| 572 | $mask |= $Bits{'all'} ; |
| 573 | $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1); |
| 574 | } |
| 575 | |
| 576 | # append 'all' when implied (empty import list or after a lone "FATAL") |
| 577 | push @_, 'all' if !@_ || @_==1 && $_[0] eq 'FATAL'; |
| 578 | |
| 579 | foreach my $word ( @_ ) { |
| 580 | if ($word eq 'FATAL') { |
| 581 | next; |
| 582 | } |
| 583 | elsif ($catmask = $Bits{$word}) { |
| 584 | $mask &= ~($catmask | $DeadBits{$word} | $All); |
| 585 | } |
| 586 | else |
| 587 | { Croaker("Unknown warnings category '$word'")} |
| 588 | } |
| 589 | |
| 590 | ${^WARNING_BITS} = $mask ; |
| 591 | } |
| 592 | |
| 593 | my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = (); |
| 594 | |
| 595 | sub MESSAGE () { 4 }; |
| 596 | sub FATAL () { 2 }; |
| 597 | sub NORMAL () { 1 }; |
| 598 | |
| 599 | sub __chk |
| 600 | { |
| 601 | my $category ; |
| 602 | my $offset ; |
| 603 | my $isobj = 0 ; |
| 604 | my $wanted = shift; |
| 605 | my $has_message = $wanted & MESSAGE; |
| 606 | |
| 607 | unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) { |
| 608 | my $sub = (caller 1)[3]; |
| 609 | my $syntax = $has_message ? "[category,] 'message'" : '[category]'; |
| 610 | Croaker("Usage: $sub($syntax)"); |
| 611 | } |
| 612 | |
| 613 | my $message = pop if $has_message; |
| 614 | |
| 615 | if (@_) { |
| 616 | # check the category supplied. |
| 617 | $category = shift ; |
| 618 | if (my $type = ref $category) { |
| 619 | Croaker("not an object") |
| 620 | if exists $builtin_type{$type}; |
| 621 | $category = $type; |
| 622 | $isobj = 1 ; |
| 623 | } |
| 624 | $offset = $Offsets{$category}; |
| 625 | Croaker("Unknown warnings category '$category'") |
| 626 | unless defined $offset; |
| 627 | } |
| 628 | else { |
| 629 | $category = (caller(1))[0] ; |
| 630 | $offset = $Offsets{$category}; |
| 631 | Croaker("package '$category' not registered for warnings") |
| 632 | unless defined $offset ; |
| 633 | } |
| 634 | |
| 635 | my $i; |
| 636 | |
| 637 | if ($isobj) { |
| 638 | my $pkg; |
| 639 | $i = 2; |
| 640 | while (do { { package DB; $pkg = (caller($i++))[0] } } ) { |
| 641 | last unless @DB::args && $DB::args[0] =~ /^$category=/ ; |
| 642 | } |
| 643 | $i -= 2 ; |
| 644 | } |
| 645 | else { |
| 646 | $i = _error_loc(); # see where Carp will allocate the error |
| 647 | } |
| 648 | |
| 649 | # Default to 0 if caller returns nothing. Default to $DEFAULT if it |
| 650 | # explicitly returns undef. |
| 651 | my(@callers_bitmask) = (caller($i))[9] ; |
| 652 | my $callers_bitmask = |
| 653 | @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ; |
| 654 | |
| 655 | my @results; |
| 656 | foreach my $type (FATAL, NORMAL) { |
| 657 | next unless $wanted & $type; |
| 658 | |
| 659 | push @results, (vec($callers_bitmask, $offset + $type - 1, 1) || |
| 660 | vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1)); |
| 661 | } |
| 662 | |
| 663 | # &enabled and &fatal_enabled |
| 664 | return $results[0] unless $has_message; |
| 665 | |
| 666 | # &warnif, and the category is neither enabled as warning nor as fatal |
| 667 | return if $wanted == (NORMAL | FATAL | MESSAGE) |
| 668 | && !($results[0] || $results[1]); |
| 669 | |
| 670 | require Carp; |
| 671 | Carp::croak($message) if $results[0]; |
| 672 | # will always get here for &warn. will only get here for &warnif if the |
| 673 | # category is enabled |
| 674 | Carp::carp($message); |
| 675 | } |
| 676 | |
| 677 | sub _mkMask |
| 678 | { |
| 679 | my ($bit) = @_; |
| 680 | my $mask = ""; |
| 681 | |
| 682 | vec($mask, $bit, 1) = 1; |
| 683 | return $mask; |
| 684 | } |
| 685 | |
| 686 | sub register_categories |
| 687 | { |
| 688 | my @names = @_; |
| 689 | |
| 690 | for my $name (@names) { |
| 691 | if (! defined $Bits{$name}) { |
| 692 | $Bits{$name} = _mkMask($LAST_BIT); |
| 693 | vec($Bits{'all'}, $LAST_BIT, 1) = 1; |
| 694 | $Offsets{$name} = $LAST_BIT ++; |
| 695 | foreach my $k (keys %Bits) { |
| 696 | vec($Bits{$k}, $LAST_BIT, 1) = 0; |
| 697 | } |
| 698 | $DeadBits{$name} = _mkMask($LAST_BIT); |
| 699 | vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | sub _error_loc { |
| 705 | require Carp; |
| 706 | goto &Carp::short_error_loc; # don't introduce another stack frame |
| 707 | } |
| 708 | |
| 709 | sub enabled |
| 710 | { |
| 711 | return __chk(NORMAL, @_); |
| 712 | } |
| 713 | |
| 714 | sub fatal_enabled |
| 715 | { |
| 716 | return __chk(FATAL, @_); |
| 717 | } |
| 718 | |
| 719 | sub warn |
| 720 | { |
| 721 | return __chk(FATAL | MESSAGE, @_); |
| 722 | } |
| 723 | |
| 724 | sub warnif |
| 725 | { |
| 726 | return __chk(NORMAL | FATAL | MESSAGE, @_); |
| 727 | } |
| 728 | |
| 729 | # These are not part of any public interface, so we can delete them to save |
| 730 | # space. |
| 731 | delete @warnings::{qw(NORMAL FATAL MESSAGE)}; |
| 732 | |
| 733 | 1; |
| 734 | __END__ |
| 735 | =head1 NAME |
| 736 | |
| 737 | warnings - Perl pragma to control optional warnings |
| 738 | |
| 739 | =head1 SYNOPSIS |
| 740 | |
| 741 | use warnings; |
| 742 | no warnings; |
| 743 | |
| 744 | use warnings "all"; |
| 745 | no warnings "all"; |
| 746 | |
| 747 | use warnings::register; |
| 748 | if (warnings::enabled()) { |
| 749 | warnings::warn("some warning"); |
| 750 | } |
| 751 | |
| 752 | if (warnings::enabled("void")) { |
| 753 | warnings::warn("void", "some warning"); |
| 754 | } |
| 755 | |
| 756 | if (warnings::enabled($object)) { |
| 757 | warnings::warn($object, "some warning"); |
| 758 | } |
| 759 | |
| 760 | warnings::warnif("some warning"); |
| 761 | warnings::warnif("void", "some warning"); |
| 762 | warnings::warnif($object, "some warning"); |
| 763 | |
| 764 | =head1 DESCRIPTION |
| 765 | |
| 766 | The C<warnings> pragma gives control over which warnings are enabled in |
| 767 | which parts of a Perl program. It's a more flexible alternative for |
| 768 | both the command line flag B<-w> and the equivalent Perl variable, |
| 769 | C<$^W>. |
| 770 | |
| 771 | This pragma works just like the C<strict> pragma. |
| 772 | This means that the scope of the warning pragma is limited to the |
| 773 | enclosing block. It also means that the pragma setting will not |
| 774 | leak across files (via C<use>, C<require> or C<do>). This allows |
| 775 | authors to independently define the degree of warning checks that will |
| 776 | be applied to their module. |
| 777 | |
| 778 | By default, optional warnings are disabled, so any legacy code that |
| 779 | doesn't attempt to control the warnings will work unchanged. |
| 780 | |
| 781 | When we talk about "all" warnings we don't actually mean "all the |
| 782 | warnings we support". See L</Top-level warning categories & associated |
| 783 | confusion> for details. The "all" category should really be called the |
| 784 | "default" category, if not for backwards-compatibility concerns. |
| 785 | |
| 786 | "All" warnings are enabled in a block by either of these: |
| 787 | |
| 788 | use warnings; |
| 789 | use warnings 'all'; |
| 790 | |
| 791 | Similarly "all" warnings are disabled in a block by either of these: |
| 792 | |
| 793 | no warnings; |
| 794 | no warnings 'all'; |
| 795 | |
| 796 | For example, consider the code below: |
| 797 | |
| 798 | use warnings; |
| 799 | my @a; |
| 800 | { |
| 801 | no warnings; |
| 802 | my $b = @a[0]; |
| 803 | } |
| 804 | my $c = @a[0]; |
| 805 | |
| 806 | The code in the enclosing block has warnings enabled, but the inner |
| 807 | block has them disabled. In this case that means the assignment to the |
| 808 | scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]"> |
| 809 | warning, but the assignment to the scalar C<$b> will not. |
| 810 | |
| 811 | =head2 Default Warnings and Optional Warnings |
| 812 | |
| 813 | Before the introduction of lexical warnings, Perl had two classes of |
| 814 | warnings: mandatory and optional. |
| 815 | |
| 816 | As its name suggests, if your code tripped a mandatory warning, you |
| 817 | would get a warning whether you wanted it or not. |
| 818 | For example, the code below would always produce an C<"isn't numeric"> |
| 819 | warning about the "2:". |
| 820 | |
| 821 | my $a = "2:" + 3; |
| 822 | |
| 823 | With the introduction of lexical warnings, mandatory warnings now become |
| 824 | I<default> warnings. The difference is that although the previously |
| 825 | mandatory warnings are still enabled by default, they can then be |
| 826 | subsequently enabled or disabled with the lexical warning pragma. For |
| 827 | example, in the code below, an C<"isn't numeric"> warning will only |
| 828 | be reported for the C<$a> variable. |
| 829 | |
| 830 | my $a = "2:" + 3; |
| 831 | no warnings; |
| 832 | my $b = "2:" + 3; |
| 833 | |
| 834 | Note that neither the B<-w> flag or the C<$^W> can be used to |
| 835 | disable/enable default warnings. They are still mandatory in this case. |
| 836 | |
| 837 | =head2 What's wrong with B<-w> and C<$^W> |
| 838 | |
| 839 | Although very useful, the big problem with using B<-w> on the command |
| 840 | line to enable warnings is that it is all or nothing. Take the typical |
| 841 | scenario when you are writing a Perl program. Parts of the code you |
| 842 | will write yourself, but it's very likely that you will make use of |
| 843 | pre-written Perl modules. If you use the B<-w> flag in this case, you |
| 844 | end up enabling warnings in pieces of code that you haven't written. |
| 845 | |
| 846 | Similarly, using C<$^W> to either disable or enable blocks of code is |
| 847 | fundamentally flawed. For a start, say you want to disable warnings in |
| 848 | a block of code. You might expect this to be enough to do the trick: |
| 849 | |
| 850 | { |
| 851 | local ($^W) = 0; |
| 852 | my $a =+ 2; |
| 853 | my $b; chop $b; |
| 854 | } |
| 855 | |
| 856 | When this code is run with the B<-w> flag, a warning will be produced |
| 857 | for the C<$a> line: C<"Reversed += operator">. |
| 858 | |
| 859 | The problem is that Perl has both compile-time and run-time warnings. To |
| 860 | disable compile-time warnings you need to rewrite the code like this: |
| 861 | |
| 862 | { |
| 863 | BEGIN { $^W = 0 } |
| 864 | my $a =+ 2; |
| 865 | my $b; chop $b; |
| 866 | } |
| 867 | |
| 868 | The other big problem with C<$^W> is the way you can inadvertently |
| 869 | change the warning setting in unexpected places in your code. For example, |
| 870 | when the code below is run (without the B<-w> flag), the second call |
| 871 | to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas |
| 872 | the first will not. |
| 873 | |
| 874 | sub doit |
| 875 | { |
| 876 | my $b; chop $b; |
| 877 | } |
| 878 | |
| 879 | doit(); |
| 880 | |
| 881 | { |
| 882 | local ($^W) = 1; |
| 883 | doit() |
| 884 | } |
| 885 | |
| 886 | This is a side-effect of C<$^W> being dynamically scoped. |
| 887 | |
| 888 | Lexical warnings get around these limitations by allowing finer control |
| 889 | over where warnings can or can't be tripped. |
| 890 | |
| 891 | =head2 Controlling Warnings from the Command Line |
| 892 | |
| 893 | There are three Command Line flags that can be used to control when |
| 894 | warnings are (or aren't) produced: |
| 895 | |
| 896 | =over 5 |
| 897 | |
| 898 | =item B<-w> |
| 899 | X<-w> |
| 900 | |
| 901 | This is the existing flag. If the lexical warnings pragma is B<not> |
| 902 | used in any of you code, or any of the modules that you use, this flag |
| 903 | will enable warnings everywhere. See L<Backward Compatibility> for |
| 904 | details of how this flag interacts with lexical warnings. |
| 905 | |
| 906 | =item B<-W> |
| 907 | X<-W> |
| 908 | |
| 909 | If the B<-W> flag is used on the command line, it will enable "all" warnings |
| 910 | throughout the program regardless of whether warnings were disabled |
| 911 | locally using C<no warnings> or C<$^W =0>. |
| 912 | This includes all files that get |
| 913 | included via C<use>, C<require> or C<do>. |
| 914 | Think of it as the Perl equivalent of the "lint" command. |
| 915 | |
| 916 | =item B<-X> |
| 917 | X<-X> |
| 918 | |
| 919 | Does the exact opposite to the B<-W> flag, i.e. it disables "all" warnings. |
| 920 | |
| 921 | =back |
| 922 | |
| 923 | =head2 Top-level warning categories & associated confusion |
| 924 | |
| 925 | The lexical warning pragma was introduced in v5.6.0 of perl, and from |
| 926 | the very beginning doing C<use warnings> would enable the "all" |
| 927 | category of warnings, which were all the warnings we support. |
| 928 | |
| 929 | This led to arguments whenever someone suggested a new warning be |
| 930 | added to perl, since that implicitly meant that existing programs that |
| 931 | used the warnings pragma would be retroactively subjected to them when |
| 932 | perl was upgraded. |
| 933 | |
| 934 | So similarly to how most C compilers support C<-Wall> to mean "not |
| 935 | quite all warnings" along with extra options like C<-Wextra>, we |
| 936 | support warnings outside of the "all" category. Think of the "all" |
| 937 | category as "default", that's what we'd call it we were starting out |
| 938 | today and didn't have a bunch of programs doing C<use warnings "all"> |
| 939 | in the wild already. |
| 940 | |
| 941 | The categories we support are: |
| 942 | |
| 943 | =over |
| 944 | |
| 945 | =item * all |
| 946 | |
| 947 | This is the "default" category for warnings that we've supported ever |
| 948 | since v5.6.0. We have and might occasionally add new warnings here if |
| 949 | they're deemed to be similar in nature to our existing warnings, but |
| 950 | mostly these are things we're pretty sure are a logic error, but |
| 951 | aren't irrecoverable, so they're not a runtime error. |
| 952 | |
| 953 | When you upgrade perl you might find that we've added some new |
| 954 | warnings here, but they won't be anything wildly different from the |
| 955 | current set of warnings, so the burden of going through your existing |
| 956 | code and auditing the new parts that are warning should be fairly |
| 957 | light. |
| 958 | |
| 959 | =item * everything |
| 960 | |
| 961 | This is what "all" would be if the world made any sense, but since we |
| 962 | started out with "all" you need to enable "everything" to really |
| 963 | enable "all the warnings". |
| 964 | |
| 965 | You almost definitely don't want to enable "everything", unless you're |
| 966 | willing to potentially get a flood of new warnings with every perl |
| 967 | upgrade, and those warnings may be entirely different in spirit to |
| 968 | existing warnings shipped with previous releases. |
| 969 | |
| 970 | Maybe we'll start introducing really pedantic warnings that aren't |
| 971 | useful for most cases, maybe we'll start warning about inconsistent |
| 972 | indentation, who knows? If you really want ALL the warnings perl has |
| 973 | to offer enable these, otherwise stick with some more sane category. |
| 974 | |
| 975 | =item * extra |
| 976 | |
| 977 | These are warnings that we might have put into "all" |
| 978 | (a.k.a. "default") if we had a time machine and were starting out with |
| 979 | perl today, but they'd probably cause too much of a disruption today |
| 980 | so we're not doing that. |
| 981 | |
| 982 | As of writing this the sole warning in this category is a warning |
| 983 | about useless use of grep in void context, but unlike for the "all" |
| 984 | category we reserve the right to freely add things to this category in |
| 985 | the future. |
| 986 | |
| 987 | =back |
| 988 | |
| 989 | In the future we might add any number of other top-level |
| 990 | categories. The backwards-compatibility promises of those categories |
| 991 | (if any) will be documented here. |
| 992 | |
| 993 | =head2 Backward Compatibility |
| 994 | |
| 995 | If you are used to working with a version of Perl prior to the |
| 996 | introduction of lexically scoped warnings, or have code that uses both |
| 997 | lexical warnings and C<$^W>, this section will describe how they interact. |
| 998 | |
| 999 | How Lexical Warnings interact with B<-w>/C<$^W>: |
| 1000 | |
| 1001 | =over 5 |
| 1002 | |
| 1003 | =item 1. |
| 1004 | |
| 1005 | If none of the three command line flags (B<-w>, B<-W> or B<-X>) that |
| 1006 | control warnings is used and neither C<$^W> nor the C<warnings> pragma |
| 1007 | are used, then default warnings will be enabled and optional warnings |
| 1008 | disabled. |
| 1009 | This means that legacy code that doesn't attempt to control the warnings |
| 1010 | will work unchanged. |
| 1011 | |
| 1012 | =item 2. |
| 1013 | |
| 1014 | The B<-w> flag just sets the global C<$^W> variable as in 5.005. This |
| 1015 | means that any legacy code that currently relies on manipulating C<$^W> |
| 1016 | to control warning behavior will still work as is. |
| 1017 | |
| 1018 | =item 3. |
| 1019 | |
| 1020 | Apart from now being a boolean, the C<$^W> variable operates in exactly |
| 1021 | the same horrible uncontrolled global way, except that it cannot |
| 1022 | disable/enable default warnings. |
| 1023 | |
| 1024 | =item 4. |
| 1025 | |
| 1026 | If a piece of code is under the control of the C<warnings> pragma, |
| 1027 | both the C<$^W> variable and the B<-w> flag will be ignored for the |
| 1028 | scope of the lexical warning. |
| 1029 | |
| 1030 | =item 5. |
| 1031 | |
| 1032 | The only way to override a lexical warnings setting is with the B<-W> |
| 1033 | or B<-X> command line flags. |
| 1034 | |
| 1035 | =back |
| 1036 | |
| 1037 | The combined effect of 3 & 4 is that it will allow code which uses |
| 1038 | the C<warnings> pragma to control the warning behavior of $^W-type |
| 1039 | code (using a C<local $^W=0>) if it really wants to, but not vice-versa. |
| 1040 | |
| 1041 | =head2 Category Hierarchy |
| 1042 | X<warning, categories> |
| 1043 | |
| 1044 | A hierarchy of "categories" have been defined to allow groups of warnings |
| 1045 | to be enabled/disabled in isolation. |
| 1046 | |
| 1047 | The current hierarchy is: |
| 1048 | |
| 1049 | =for warnings.pl tree-goes-here |
| 1050 | |
| 1051 | Just like the "strict" pragma any of these categories can be combined |
| 1052 | |
| 1053 | use warnings qw(void redefine); |
| 1054 | no warnings qw(io syntax untie); |
| 1055 | |
| 1056 | Also like the "strict" pragma, if there is more than one instance of the |
| 1057 | C<warnings> pragma in a given scope the cumulative effect is additive. |
| 1058 | |
| 1059 | use warnings qw(void); # only "void" warnings enabled |
| 1060 | ... |
| 1061 | use warnings qw(io); # only "void" & "io" warnings enabled |
| 1062 | ... |
| 1063 | no warnings qw(void); # only "io" warnings enabled |
| 1064 | |
| 1065 | To determine which category a specific warning has been assigned to see |
| 1066 | L<perldiag>. |
| 1067 | |
| 1068 | Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a |
| 1069 | sub-category of the "syntax" category. It is now a top-level category |
| 1070 | in its own right. |
| 1071 | |
| 1072 | Note: Before 5.21.0, the "missing" lexical warnings category was |
| 1073 | internally defined to be the same as the "uninitialized" category. It |
| 1074 | is now a top-level category in its own right. |
| 1075 | |
| 1076 | =head2 Fatal Warnings |
| 1077 | X<warning, fatal> |
| 1078 | |
| 1079 | The presence of the word "FATAL" in the category list will escalate any |
| 1080 | warnings detected from the categories specified in the lexical scope |
| 1081 | into fatal errors. In the code below, the use of C<time>, C<length> |
| 1082 | and C<join> can all produce a C<"Useless use of xxx in void context"> |
| 1083 | warning. |
| 1084 | |
| 1085 | use warnings; |
| 1086 | |
| 1087 | time; |
| 1088 | |
| 1089 | { |
| 1090 | use warnings FATAL => qw(void); |
| 1091 | length "abc"; |
| 1092 | } |
| 1093 | |
| 1094 | join "", 1,2,3; |
| 1095 | |
| 1096 | print "done\n"; |
| 1097 | |
| 1098 | When run it produces this output |
| 1099 | |
| 1100 | Useless use of time in void context at fatal line 3. |
| 1101 | Useless use of length in void context at fatal line 7. |
| 1102 | |
| 1103 | The scope where C<length> is used has escalated the C<void> warnings |
| 1104 | category into a fatal error, so the program terminates immediately when it |
| 1105 | encounters the warning. |
| 1106 | |
| 1107 | To explicitly turn off a "FATAL" warning you just disable the warning |
| 1108 | it is associated with. So, for example, to disable the "void" warning |
| 1109 | in the example above, either of these will do the trick: |
| 1110 | |
| 1111 | no warnings qw(void); |
| 1112 | no warnings FATAL => qw(void); |
| 1113 | |
| 1114 | If you want to downgrade a warning that has been escalated into a fatal |
| 1115 | error back to a normal warning, you can use the "NONFATAL" keyword. For |
| 1116 | example, the code below will promote all warnings into fatal errors, |
| 1117 | except for those in the "syntax" category. |
| 1118 | |
| 1119 | use warnings FATAL => 'all', NONFATAL => 'syntax'; |
| 1120 | |
| 1121 | As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can |
| 1122 | use: |
| 1123 | |
| 1124 | use v5.20; # Perl 5.20 or greater is required for the following |
| 1125 | use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';" |
| 1126 | |
| 1127 | If you want your program to be compatible with versions of Perl before |
| 1128 | 5.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In |
| 1129 | previous versions of Perl, the behavior of the statements |
| 1130 | C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and |
| 1131 | C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if |
| 1132 | they included the C<< => 'all' >> portion. As of 5.20, they do.) |
| 1133 | |
| 1134 | B<NOTE:> Users of FATAL warnings, especially |
| 1135 | those using C<< FATAL => 'all' >> |
| 1136 | should be fully aware that they are risking future portability of their |
| 1137 | programs by doing so. Perl makes absolutely no commitments to not |
| 1138 | introduce new warnings, or warnings categories in the future, and indeed |
| 1139 | we explicitly reserve the right to do so. Code that may not warn now may |
| 1140 | warn in a future release of Perl if the Perl5 development team deems it |
| 1141 | in the best interests of the community to do so. Should code using FATAL |
| 1142 | warnings break due to the introduction of a new warning we will NOT |
| 1143 | consider it an incompatible change. Users of FATAL warnings should take |
| 1144 | special caution during upgrades to check to see if their code triggers |
| 1145 | any new warnings and should pay particular attention to the fine print of |
| 1146 | the documentation of the features they use to ensure they do not exploit |
| 1147 | features that are documented as risky, deprecated, or unspecified, or where |
| 1148 | the documentation says "so don't do that", or anything with the same sense |
| 1149 | and spirit. Use of such features in combination with FATAL warnings is |
| 1150 | ENTIRELY AT THE USER'S RISK. |
| 1151 | |
| 1152 | =head2 Reporting Warnings from a Module |
| 1153 | X<warning, reporting> X<warning, registering> |
| 1154 | |
| 1155 | The C<warnings> pragma provides a number of functions that are useful for |
| 1156 | module authors. These are used when you want to report a module-specific |
| 1157 | warning to a calling module has enabled warnings via the C<warnings> |
| 1158 | pragma. |
| 1159 | |
| 1160 | Consider the module C<MyMod::Abc> below. |
| 1161 | |
| 1162 | package MyMod::Abc; |
| 1163 | |
| 1164 | use warnings::register; |
| 1165 | |
| 1166 | sub open { |
| 1167 | my $path = shift; |
| 1168 | if ($path !~ m#^/#) { |
| 1169 | warnings::warn("changing relative path to /var/abc") |
| 1170 | if warnings::enabled(); |
| 1171 | $path = "/var/abc/$path"; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | 1; |
| 1176 | |
| 1177 | The call to C<warnings::register> will create a new warnings category |
| 1178 | called "MyMod::Abc", i.e. the new category name matches the current |
| 1179 | package name. The C<open> function in the module will display a warning |
| 1180 | message if it gets given a relative path as a parameter. This warnings |
| 1181 | will only be displayed if the code that uses C<MyMod::Abc> has actually |
| 1182 | enabled them with the C<warnings> pragma like below. |
| 1183 | |
| 1184 | use MyMod::Abc; |
| 1185 | use warnings 'MyMod::Abc'; |
| 1186 | ... |
| 1187 | abc::open("../fred.txt"); |
| 1188 | |
| 1189 | It is also possible to test whether the pre-defined warnings categories are |
| 1190 | set in the calling module with the C<warnings::enabled> function. Consider |
| 1191 | this snippet of code: |
| 1192 | |
| 1193 | package MyMod::Abc; |
| 1194 | |
| 1195 | sub open { |
| 1196 | warnings::warnif("deprecated", |
| 1197 | "open is deprecated, use new instead"); |
| 1198 | new(@_); |
| 1199 | } |
| 1200 | |
| 1201 | sub new |
| 1202 | ... |
| 1203 | 1; |
| 1204 | |
| 1205 | The function C<open> has been deprecated, so code has been included to |
| 1206 | display a warning message whenever the calling module has (at least) the |
| 1207 | "deprecated" warnings category enabled. Something like this, say. |
| 1208 | |
| 1209 | use warnings 'deprecated'; |
| 1210 | use MyMod::Abc; |
| 1211 | ... |
| 1212 | MyMod::Abc::open($filename); |
| 1213 | |
| 1214 | Either the C<warnings::warn> or C<warnings::warnif> function should be |
| 1215 | used to actually display the warnings message. This is because they can |
| 1216 | make use of the feature that allows warnings to be escalated into fatal |
| 1217 | errors. So in this case |
| 1218 | |
| 1219 | use MyMod::Abc; |
| 1220 | use warnings FATAL => 'MyMod::Abc'; |
| 1221 | ... |
| 1222 | MyMod::Abc::open('../fred.txt'); |
| 1223 | |
| 1224 | the C<warnings::warnif> function will detect this and die after |
| 1225 | displaying the warning message. |
| 1226 | |
| 1227 | The three warnings functions, C<warnings::warn>, C<warnings::warnif> |
| 1228 | and C<warnings::enabled> can optionally take an object reference in place |
| 1229 | of a category name. In this case the functions will use the class name |
| 1230 | of the object as the warnings category. |
| 1231 | |
| 1232 | Consider this example: |
| 1233 | |
| 1234 | package Original; |
| 1235 | |
| 1236 | no warnings; |
| 1237 | use warnings::register; |
| 1238 | |
| 1239 | sub new |
| 1240 | { |
| 1241 | my $class = shift; |
| 1242 | bless [], $class; |
| 1243 | } |
| 1244 | |
| 1245 | sub check |
| 1246 | { |
| 1247 | my $self = shift; |
| 1248 | my $value = shift; |
| 1249 | |
| 1250 | if ($value % 2 && warnings::enabled($self)) |
| 1251 | { warnings::warn($self, "Odd numbers are unsafe") } |
| 1252 | } |
| 1253 | |
| 1254 | sub doit |
| 1255 | { |
| 1256 | my $self = shift; |
| 1257 | my $value = shift; |
| 1258 | $self->check($value); |
| 1259 | # ... |
| 1260 | } |
| 1261 | |
| 1262 | 1; |
| 1263 | |
| 1264 | package Derived; |
| 1265 | |
| 1266 | use warnings::register; |
| 1267 | use Original; |
| 1268 | our @ISA = qw( Original ); |
| 1269 | sub new |
| 1270 | { |
| 1271 | my $class = shift; |
| 1272 | bless [], $class; |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | 1; |
| 1277 | |
| 1278 | The code below makes use of both modules, but it only enables warnings from |
| 1279 | C<Derived>. |
| 1280 | |
| 1281 | use Original; |
| 1282 | use Derived; |
| 1283 | use warnings 'Derived'; |
| 1284 | my $a = Original->new(); |
| 1285 | $a->doit(1); |
| 1286 | my $b = Derived->new(); |
| 1287 | $a->doit(1); |
| 1288 | |
| 1289 | When this code is run only the C<Derived> object, C<$b>, will generate |
| 1290 | a warning. |
| 1291 | |
| 1292 | Odd numbers are unsafe at main.pl line 7 |
| 1293 | |
| 1294 | Notice also that the warning is reported at the line where the object is first |
| 1295 | used. |
| 1296 | |
| 1297 | When registering new categories of warning, you can supply more names to |
| 1298 | warnings::register like this: |
| 1299 | |
| 1300 | package MyModule; |
| 1301 | use warnings::register qw(format precision); |
| 1302 | |
| 1303 | ... |
| 1304 | |
| 1305 | warnings::warnif('MyModule::format', '...'); |
| 1306 | |
| 1307 | =head1 FUNCTIONS |
| 1308 | |
| 1309 | =over 4 |
| 1310 | |
| 1311 | =item use warnings::register |
| 1312 | |
| 1313 | Creates a new warnings category with the same name as the package where |
| 1314 | the call to the pragma is used. |
| 1315 | |
| 1316 | =item warnings::enabled() |
| 1317 | |
| 1318 | Use the warnings category with the same name as the current package. |
| 1319 | |
| 1320 | Return TRUE if that warnings category is enabled in the calling module. |
| 1321 | Otherwise returns FALSE. |
| 1322 | |
| 1323 | =item warnings::enabled($category) |
| 1324 | |
| 1325 | Return TRUE if the warnings category, C<$category>, is enabled in the |
| 1326 | calling module. |
| 1327 | Otherwise returns FALSE. |
| 1328 | |
| 1329 | =item warnings::enabled($object) |
| 1330 | |
| 1331 | Use the name of the class for the object reference, C<$object>, as the |
| 1332 | warnings category. |
| 1333 | |
| 1334 | Return TRUE if that warnings category is enabled in the first scope |
| 1335 | where the object is used. |
| 1336 | Otherwise returns FALSE. |
| 1337 | |
| 1338 | =item warnings::fatal_enabled() |
| 1339 | |
| 1340 | Return TRUE if the warnings category with the same name as the current |
| 1341 | package has been set to FATAL in the calling module. |
| 1342 | Otherwise returns FALSE. |
| 1343 | |
| 1344 | =item warnings::fatal_enabled($category) |
| 1345 | |
| 1346 | Return TRUE if the warnings category C<$category> has been set to FATAL in |
| 1347 | the calling module. |
| 1348 | Otherwise returns FALSE. |
| 1349 | |
| 1350 | =item warnings::fatal_enabled($object) |
| 1351 | |
| 1352 | Use the name of the class for the object reference, C<$object>, as the |
| 1353 | warnings category. |
| 1354 | |
| 1355 | Return TRUE if that warnings category has been set to FATAL in the first |
| 1356 | scope where the object is used. |
| 1357 | Otherwise returns FALSE. |
| 1358 | |
| 1359 | =item warnings::warn($message) |
| 1360 | |
| 1361 | Print C<$message> to STDERR. |
| 1362 | |
| 1363 | Use the warnings category with the same name as the current package. |
| 1364 | |
| 1365 | If that warnings category has been set to "FATAL" in the calling module |
| 1366 | then die. Otherwise return. |
| 1367 | |
| 1368 | =item warnings::warn($category, $message) |
| 1369 | |
| 1370 | Print C<$message> to STDERR. |
| 1371 | |
| 1372 | If the warnings category, C<$category>, has been set to "FATAL" in the |
| 1373 | calling module then die. Otherwise return. |
| 1374 | |
| 1375 | =item warnings::warn($object, $message) |
| 1376 | |
| 1377 | Print C<$message> to STDERR. |
| 1378 | |
| 1379 | Use the name of the class for the object reference, C<$object>, as the |
| 1380 | warnings category. |
| 1381 | |
| 1382 | If that warnings category has been set to "FATAL" in the scope where C<$object> |
| 1383 | is first used then die. Otherwise return. |
| 1384 | |
| 1385 | |
| 1386 | =item warnings::warnif($message) |
| 1387 | |
| 1388 | Equivalent to: |
| 1389 | |
| 1390 | if (warnings::enabled()) |
| 1391 | { warnings::warn($message) } |
| 1392 | |
| 1393 | =item warnings::warnif($category, $message) |
| 1394 | |
| 1395 | Equivalent to: |
| 1396 | |
| 1397 | if (warnings::enabled($category)) |
| 1398 | { warnings::warn($category, $message) } |
| 1399 | |
| 1400 | =item warnings::warnif($object, $message) |
| 1401 | |
| 1402 | Equivalent to: |
| 1403 | |
| 1404 | if (warnings::enabled($object)) |
| 1405 | { warnings::warn($object, $message) } |
| 1406 | |
| 1407 | =item warnings::register_categories(@names) |
| 1408 | |
| 1409 | This registers warning categories for the given names and is primarily for |
| 1410 | use by the warnings::register pragma. |
| 1411 | |
| 1412 | =back |
| 1413 | |
| 1414 | See also L<perlmodlib/Pragmatic Modules> and L<perldiag>. |
| 1415 | |
| 1416 | =cut |