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 $WARNING_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 $WARNING_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.65'; 20 21BEGIN { 22 require './regen/regen_lib.pl'; 23 push @INC, './lib'; 24} 25use strict ; 26 27sub DEFAULT_ON () { 1 } 28sub DEFAULT_OFF () { 2 } 29 30 31# Define the hierarchy of warnings. 32# 33# Each level in the tree is a hash which lists the names of all the 34# children below that level. Each child is an array consisting of the 35# version when that warnings category was introduced and, if a terminal 36# category, whether that warning is on by default; otherwise a ref to 37# another hash of children. 38# 39# Note that the version numbers are currently only used to sort and to 40# generate code comments in the output files. 41# 42# Note that warning names aren't hierarchical; by having 'pipe' as a child 43# of 'io', a warnings category called 'io::pipe' is NOT automatically 44# created. But the warnings category 'io' WILL include all the mask bits 45# necessary to turn on 'pipe', 'unopened' etc. 46 47our $WARNING_TREE = { 48'all' => [ 5.008, { 49 'io' => [ 5.008, { 50 'pipe' => [ 5.008, DEFAULT_OFF], 51 'unopened' => [ 5.008, DEFAULT_OFF], 52 'closed' => [ 5.008, DEFAULT_OFF], 53 'newline' => [ 5.008, DEFAULT_OFF], 54 'exec' => [ 5.008, DEFAULT_OFF], 55 'layer' => [ 5.008, DEFAULT_OFF], 56 'syscalls' => [ 5.019, DEFAULT_OFF], 57 }], 58 'syntax' => [ 5.008, { 59 'ambiguous' => [ 5.008, DEFAULT_OFF], 60 'semicolon' => [ 5.008, DEFAULT_OFF], 61 'precedence' => [ 5.008, DEFAULT_OFF], 62 'bareword' => [ 5.008, DEFAULT_OFF], 63 'reserved' => [ 5.008, DEFAULT_OFF], 64 'digit' => [ 5.008, DEFAULT_OFF], 65 'parenthesis' => [ 5.008, DEFAULT_OFF], 66 'printf' => [ 5.008, DEFAULT_OFF], 67 'prototype' => [ 5.008, DEFAULT_OFF], 68 'qw' => [ 5.008, DEFAULT_OFF], 69 'illegalproto' => [ 5.011, DEFAULT_OFF], 70 }], 71 'severe' => [ 5.008, { 72 'inplace' => [ 5.008, DEFAULT_ON], 73 'internal' => [ 5.008, DEFAULT_OFF], 74 'debugging' => [ 5.008, DEFAULT_ON], 75 'malloc' => [ 5.008, DEFAULT_ON], 76 }], 77 'deprecated' => [ 5.008, DEFAULT_ON, { 78 'deprecated::goto_construct' => [ 5.011003, DEFAULT_ON], 79 'deprecated::unicode_property_name' => [ 5.011003, DEFAULT_ON], 80 'deprecated::dot_in_inc' => [ 5.025011, DEFAULT_ON], 81 'deprecated::version_downgrade' => [ 5.035009, DEFAULT_ON], 82 'deprecated::delimiter_will_be_paired' => [ 5.035010, DEFAULT_ON], 83 'deprecated::apostrophe_as_package_separator' 84 => [ 5.037009, DEFAULT_ON], 85 'deprecated::smartmatch' => [ 5.037010, DEFAULT_ON], 86 }], 87 'void' => [ 5.008, DEFAULT_OFF], 88 'recursion' => [ 5.008, DEFAULT_OFF], 89 'redefine' => [ 5.008, DEFAULT_OFF], 90 'numeric' => [ 5.008, DEFAULT_OFF], 91 'uninitialized' => [ 5.008, DEFAULT_OFF], 92 'once' => [ 5.008, DEFAULT_OFF], 93 'misc' => [ 5.008, DEFAULT_OFF], 94 'regexp' => [ 5.008, DEFAULT_OFF], 95 'glob' => [ 5.008, DEFAULT_ON], 96 'untie' => [ 5.008, DEFAULT_OFF], 97 'substr' => [ 5.008, DEFAULT_OFF], 98 'taint' => [ 5.008, DEFAULT_OFF], 99 'signal' => [ 5.008, DEFAULT_OFF], 100 'closure' => [ 5.008, DEFAULT_OFF], 101 'overflow' => [ 5.008, DEFAULT_OFF], 102 'portable' => [ 5.008, DEFAULT_OFF], 103 'utf8' => [ 5.008, { 104 'surrogate' => [ 5.013, DEFAULT_OFF], 105 'nonchar' => [ 5.013, DEFAULT_OFF], 106 'non_unicode' => [ 5.013, DEFAULT_OFF], 107 }], 108 'exiting' => [ 5.008, DEFAULT_OFF], 109 'pack' => [ 5.008, DEFAULT_OFF], 110 'unpack' => [ 5.008, DEFAULT_OFF], 111 'threads' => [ 5.008, DEFAULT_OFF], 112 'imprecision' => [ 5.011, DEFAULT_OFF], 113 'experimental' => [ 5.017, { 114 'experimental::lexical_subs' => 115 [ 5.017, DEFAULT_ON ], 116 'experimental::regex_sets' => 117 [ 5.017, DEFAULT_OFF ], 118 'experimental::smartmatch' => 119 [ 5.017, DEFAULT_ON ], 120 'experimental::postderef' => 121 [ 5.019, DEFAULT_ON ], 122 'experimental::signatures' => 123 [ 5.019, DEFAULT_ON ], 124 'experimental::refaliasing' => 125 [ 5.021, DEFAULT_ON ], 126 'experimental::re_strict' => 127 [ 5.021, DEFAULT_ON ], 128 'experimental::const_attr' => 129 [ 5.021, DEFAULT_ON ], 130 'experimental::bitwise' => 131 [ 5.021, DEFAULT_ON ], 132 'experimental::declared_refs' => 133 [ 5.025, DEFAULT_ON ], 134 'experimental::script_run' => 135 [ 5.027, DEFAULT_ON ], 136 'experimental::alpha_assertions' => 137 [ 5.027, DEFAULT_ON ], 138 'experimental::private_use' => 139 [ 5.029, DEFAULT_ON ], 140 'experimental::uniprop_wildcards' => 141 [ 5.029, DEFAULT_ON ], 142 'experimental::vlb' => 143 [ 5.029, DEFAULT_ON ], 144 'experimental::isa' => 145 [ 5.031, DEFAULT_ON ], 146 'experimental::try' => 147 [ 5.033, DEFAULT_ON ], 148 'experimental::defer' => 149 [ 5.035, DEFAULT_ON ], 150 'experimental::for_list' => 151 [ 5.035, DEFAULT_ON ], 152 'experimental::builtin' => 153 [ 5.035, DEFAULT_ON ], 154 'experimental::args_array_with_signatures' => 155 [ 5.035, DEFAULT_ON], 156 'experimental::extra_paired_delimiters' => 157 [ 5.035, DEFAULT_ON], 158 'experimental::class' => 159 [ 5.037, DEFAULT_ON ], 160 }], 161 162 'missing' => [ 5.021, DEFAULT_OFF], 163 'redundant' => [ 5.021, DEFAULT_OFF], 164 'locale' => [ 5.021, DEFAULT_ON], 165 'shadow' => [ 5.027, DEFAULT_OFF], 166 'scalar' => [ 5.035, DEFAULT_OFF], 167 168 #'default' => [ 5.008, DEFAULT_ON ], 169}]}; 170 171 172 173my @DEFAULTS; # List of category numbers which are DEFAULT_ON 174 175 # for each category name, list which category number(s) 176 # it enables; e.g. 177my %CATEGORIES; # { 'name' => [ 1,2,5], ... } 178 179my %VALUE_TO_NAME; # (index_number => [ 'NAME', version ], ...); 180 181my %NAME_TO_VALUE; # ('NAME' => index_number, ....); 182 183# the experiments were successful (or abandonned), 184# so no warning bit is needed anymore 185my %NO_BIT_FOR = map { ( uc $_ => 1, $_ => 1 ) } qw( 186 experimental::lexical_subs 187 experimental::postderef 188 experimental::signatures 189 experimental::bitwise 190 experimental::alpha_assertions 191 experimental::script_run 192 experimental::isa 193 experimental::smartmatch 194); 195 196########################################################################### 197 198# Generate a hash with keys being the version number and values 199# being a list of node names with that version, e.g. 200# 201# { '5.008' => [ 'all', 'closure', .. ], 5.021' => .... } 202# 203# A ref to the (initially empty) hash is passed as an arg, which is 204# recursively populated 205 206sub valueWalk 207{ 208 my ($tree, $v_list) = @_; 209 my ($k, $v) ; 210 211 foreach $k (sort keys %$tree) { 212 $v = $tree->{$k}; 213 die "Value associated with key '$k' is not an ARRAY reference" 214 if !ref $v || ref $v ne 'ARRAY' ; 215 216 my ($ver, $rest, $rest2) = @{ $v } ; 217 my $ref = ref $rest ? $rest : $rest2; 218 push @{ $v_list->{$ver} }, $k; 219 220 if (ref $ref) 221 { valueWalk ($ref, $v_list) } 222 } 223} 224 225 226# Assign an index number to each category, ordered by introduced-version. 227# Populate: 228# 229# %VALUE_TO_NAME = (index_number => [ 'NAME', version ], ...); 230# %NAME_TO_VALUE = ('NAME' => index_number, ....); 231# 232# Returns count of categories. 233 234 235sub orderValues 236{ 237 my ($tree) = @_; 238 239 my %v_list; 240 valueWalk($tree, \%v_list); 241 242 my $index = 0; 243 foreach my $ver ( sort { $a <=> $b } keys %v_list ) { 244 foreach my $name (@{ $v_list{$ver} } ) { 245 next if $NO_BIT_FOR{$name}; 246 $VALUE_TO_NAME{ $index } = [ uc $name, $ver ] ; 247 $NAME_TO_VALUE{ uc $name } = $index ++ ; 248 } 249 } 250 251 return $index ; 252} 253 254 255########################################################################### 256 257# Recurse the tree and populate 258# %CATEGORIES 259# %DEFAULTS 260 261sub walk 262{ 263 my $tree = shift ; 264 my @list = () ; 265 my ($k, $v) ; 266 267 foreach $k (sort keys %$tree) { 268 $v = $tree->{$k}; 269 die "duplicate key $k\n" if defined $CATEGORIES{$k} ; 270 next if $NO_BIT_FOR{$k}; 271 die "Can't find key '$k'" 272 if ! defined $NAME_TO_VALUE{uc $k} ; 273 push @{ $CATEGORIES{$k} }, $NAME_TO_VALUE{uc $k} ; 274 die "Value associated with key '$k' is not an ARRAY reference" 275 if !ref $v || ref $v ne 'ARRAY' ; 276 277 my ($ver, $rest, $rest2) = @{ $v } ; 278 my $ref = ref $rest ? $rest : $rest2; 279 if (!ref $rest and $rest == DEFAULT_ON) 280 { push @DEFAULTS, $NAME_TO_VALUE{uc $k} } 281 if (ref $ref) 282 { push (@{ $CATEGORIES{$k} }, walk ($ref)) } 283 284 push @list, @{ $CATEGORIES{$k} } ; 285 } 286 287 return @list ; 288} 289 290 291########################################################################### 292 293# convert a list like (1,2,3,7,8) into a string like '1..3,7,8' 294 295sub mkRange 296{ 297 my @in = @_ ; 298 my @out = @in ; 299 300 for my $i (1 .. @in - 1) { 301 $out[$i] = ".." 302 if $in[$i] == $in[$i - 1] + 1 303 && ($i >= @in - 1 || $in[$i] + 1 == $in[$i + 1] ); 304 } 305 $out[-1] = $in[-1] if $out[-1] eq ".."; 306 307 my $out = join(",",@out); 308 309 $out =~ s/,(\.\.,)+/../g ; 310 return $out; 311} 312 313 314########################################################################### 315 316# return a string containing a visual representation of the warnings tree 317# structure. 318 319sub warningsTree 320{ 321 my $tree = shift ; 322 my $prefix = shift ; 323 my ($k, $v) ; 324 325 my $max = (sort {$a <=> $b} map { length $_ } keys %$tree)[-1] ; 326 my @keys = sort keys %$tree ; 327 328 my $rv = ''; 329 330 while ($k = shift @keys) { 331 next if $NO_BIT_FOR{$k}; 332 $v = $tree->{$k}; 333 die "Value associated with key '$k' is not an ARRAY reference" 334 if !ref $v || ref $v ne 'ARRAY' ; 335 336 my $offset ; 337 if ($tree ne $WARNING_TREE) { 338 $rv .= $prefix . "|\n" ; 339 $rv .= $prefix . "+- $k" ; 340 $offset = ' ' x ($max + 4) ; 341 } 342 else { 343 $rv .= $prefix . "$k" ; 344 $offset = ' ' x ($max + 1) ; 345 } 346 347 my ($ver, $rest, $rest2) = @{ $v } ; 348 my $ref = ref $rest ? $rest : $rest2; 349 if (ref $ref) 350 { 351 my $bar = @keys ? "|" : " "; 352 $rv .= " -" . "-" x ($max - length $k ) . "+\n" ; 353 $rv .= warningsTree ($ref, $prefix . $bar . $offset ) 354 } 355 else 356 { $rv .= "\n" } 357 } 358 359 return $rv; 360} 361 362 363########################################################################### 364 365# common backend for mkHex() and mkOct() 366 367sub mkHexOct 368{ 369 my ($f, $max, @bits) = @_ ; 370 my $mask = "\x00" x $max ; 371 my $string = "" ; 372 373 foreach (@bits) { 374 vec($mask, $_, 1) = 1 ; 375 } 376 377 foreach (unpack("C*", $mask)) { 378 if ($f eq 'x') { 379 $string .= '\x' . sprintf("%2.2x", $_) 380 } 381 else { 382 $string .= '\\' . sprintf("%o", $_) 383 } 384 } 385 return $string ; 386} 387 388# Convert a list of bit offsets (0...) into a string containing $max bytes 389# of the form "\xMM\xNN...." 390 391sub mkHex 392{ 393 my($max, @bits) = @_; 394 return mkHexOct("x", $max, @bits); 395} 396 397# Like mkHex(), but outputs "\o..." instead 398 399sub mkOct 400{ 401 my($max, @bits) = @_; 402 return mkHexOct("o", $max, @bits); 403} 404 405 406########################################################################### 407sub main { 408 409 if (@ARGV && $ARGV[0] eq "tree") 410 { 411 print warningsTree($WARNING_TREE, " ") ; 412 exit ; 413 } 414 415 my ($warn_h, $warn_pm) = map { 416 open_new($_, '>', { by => 'regen/warnings.pl' }); 417 } 'warnings.h', 'lib/warnings.pm'; 418 419 my ($index, $warn_size); 420 421 # generate warnings.h 422 423 print $warn_h warnings_h_boilerplate_1(); 424 425 $index = orderValues($WARNING_TREE); 426 427 die <<~EOM if $index > 255 ; 428 Too many warnings categories -- max is 255 429 rewrite packWARN* & unpackWARN* macros 430 EOM 431 432 walk ($WARNING_TREE) ; 433 for (my $i = $index; $i & 3; $i++) { 434 push @{$CATEGORIES{all}}, $i; 435 } 436 437 $index *= 2 ; 438 $warn_size = int($index / 8) + ($index % 8 != 0) ; 439 440 my $k ; 441 my $last_ver = 0; 442 my @names; 443 foreach $k (sort { $a <=> $b } keys %VALUE_TO_NAME) { 444 my ($name, $version) = @{ $VALUE_TO_NAME{$k} }; 445 print $warn_h "\n/* Warnings Categories added in Perl $version */\n\n" 446 if $last_ver != $version ; 447 $name =~ y/:/_/; 448 $name = "WARN_$name"; 449 print $warn_h tab(6, "#define $name"), " $k\n" ; 450 push @names, $name; 451 $last_ver = $version ; 452 } 453 454 print $warn_h tab(6, '#define WARNsize'), " $warn_size\n" ; 455 print $warn_h tab(6, '#define WARN_ALLstring'), ' "', ('\125' x $warn_size) , "\"\n" ; 456 print $warn_h tab(6, '#define WARN_NONEstring'), ' "', ('\0' x $warn_size) , "\"\n" ; 457 458 print $warn_h warnings_h_boilerplate_2(); 459 460 print $warn_h "\n\n/*\n" ; 461 print $warn_h map { "=for apidoc Amnh||$_\n" } @names; 462 print $warn_h "\n=cut\n*/\n\n" ; 463 print $warn_h "/* end of file warnings.h */\n"; 464 465 read_only_bottom_close_and_rename($warn_h); 466 467 468 # generate warnings.pm 469 470 while (<DATA>) { 471 last if /^VERSION$/ ; 472 print $warn_pm $_ ; 473 } 474 475 print $warn_pm qq(our \$VERSION = "$::VERSION";\n); 476 477 while (<DATA>) { 478 last if /^KEYWORDS$/ ; 479 print $warn_pm $_ ; 480 } 481 482 $last_ver = 0; 483 print $warn_pm "our %Offsets = (" ; 484 foreach my $k (sort { $a <=> $b } keys %VALUE_TO_NAME) { 485 my ($name, $version) = @{ $VALUE_TO_NAME{$k} }; 486 $name = lc $name; 487 $k *= 2 ; 488 if ( $last_ver != $version ) { 489 print $warn_pm "\n"; 490 print $warn_pm tab(6, " # Warnings Categories added in Perl $version"); 491 print $warn_pm "\n"; 492 } 493 print $warn_pm tab(6, " '$name'"), "=> $k,\n" ; 494 $last_ver = $version; 495 } 496 497 print $warn_pm ");\n\n" ; 498 499 print $warn_pm "our %Bits = (\n" ; 500 foreach my $k (sort keys %CATEGORIES) { 501 502 my $v = $CATEGORIES{$k} ; 503 my @list = sort { $a <=> $b } @$v ; 504 505 print $warn_pm tab(6, " '$k'"), '=> "', 506 mkHex($warn_size, map $_ * 2 , @list), 507 '", # [', mkRange(@list), "]\n" ; 508 } 509 510 print $warn_pm ");\n\n" ; 511 512 print $warn_pm "our %DeadBits = (\n" ; 513 foreach my $k (sort keys %CATEGORIES) { 514 515 my $v = $CATEGORIES{$k} ; 516 my @list = sort { $a <=> $b } @$v ; 517 518 print $warn_pm tab(6, " '$k'"), '=> "', 519 mkHex($warn_size, map $_ * 2 + 1 , @list), 520 '", # [', mkRange(@list), "]\n" ; 521 } 522 523 print $warn_pm ");\n\n" ; 524 525 print $warn_pm "our %NoOp = (\n" ; 526 foreach my $k ( grep /\A[a-z:_]+\z/, sort keys %NO_BIT_FOR ) { 527 print $warn_pm tab(6, " '$k'"), "=> 1,\n"; 528 } 529 530 print $warn_pm ");\n\n" ; 531 print $warn_pm "# These are used by various things, including our own tests\n"; 532 print $warn_pm tab(6, 'our $NONE'), '= "', ('\0' x $warn_size) , "\";\n" ; 533 print $warn_pm tab(6, 'our $DEFAULT'), '= "', 534 mkHex($warn_size, map $_ * 2, @DEFAULTS), 535 '"; # [', mkRange(sort { $a <=> $b } @DEFAULTS), "]\n" ; 536 print $warn_pm tab(6, 'our $LAST_BIT'), '= ' . "$index ;\n" ; 537 print $warn_pm tab(6, 'our $BYTES'), '= ' . "$warn_size ;\n" ; 538 while (<DATA>) { 539 if ($_ eq "=for warnings.pl tree-goes-here\n") { 540 print $warn_pm warningsTree($WARNING_TREE, " "); 541 next; 542 } 543 print $warn_pm $_ ; 544 } 545 546 read_only_bottom_close_and_rename($warn_pm); 547 548 exit(0); 549} 550 551main() unless caller(); 552# ----------------------------------------------------------------- 553 554sub warnings_h_boilerplate_1 { return <<'EOM'; } 555 556#define Perl_Warn_Off_(x) ((x) / 8) 557#define Perl_Warn_Bit_(x) (1 << ((x) % 8)) 558#define PerlWarnIsSet_(a, x) ((a)[Perl_Warn_Off_(x)] & Perl_Warn_Bit_(x)) 559 560#define G_WARN_OFF 0 /* $^W == 0 */ 561#define G_WARN_ON 1 /* -w flag and $^W != 0 */ 562#define G_WARN_ALL_ON 2 /* -W flag */ 563#define G_WARN_ALL_OFF 4 /* -X flag */ 564#define G_WARN_ONCE 8 /* set if 'once' ever enabled */ 565#define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF) 566 567#define pWARN_STD NULL 568#define pWARN_ALL &PL_WARN_ALL /* use warnings 'all' */ 569#define pWARN_NONE &PL_WARN_NONE /* no warnings 'all' */ 570 571#define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \ 572 (x) == pWARN_NONE) 573 574/* if PL_warnhook is set to this value, then warnings die */ 575#define PERL_WARNHOOK_FATAL (&PL_sv_placeholder) 576EOM 577 578# ----------------------------------------------------------------- 579 580sub warnings_h_boilerplate_2 { return <<'EOM'; } 581 582#define isLEXWARN_on \ 583 cBOOL(PL_curcop && PL_curcop->cop_warnings != pWARN_STD) 584#define isLEXWARN_off \ 585 cBOOL(!PL_curcop || PL_curcop->cop_warnings == pWARN_STD) 586#define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE)) 587#define hasWARNBIT(c,x) (RCPV_LEN(c) > (2*(x)/8)) 588#define isWARN_on(c,x) (hasWARNBIT(c,x) \ 589 ? PerlWarnIsSet_((U8 *)(c), 2*(x)) \ 590 : 0) 591#define isWARNf_on(c,x) (hasWARNBIT(c,x) \ 592 ? PerlWarnIsSet_((U8 *)(c), 2*(x)+1) \ 593 : 0) 594 595#define DUP_WARNINGS(p) Perl_dup_warnings(aTHX_ p) 596 597#define free_and_set_cop_warnings(cmp,w) STMT_START { \ 598 if (!specialWARN((cmp)->cop_warnings)) rcpv_free((cmp)->cop_warnings); \ 599 (cmp)->cop_warnings = w; \ 600} STMT_END 601 602/* 603 604=head1 Warning and Dieing 605 606In all these calls, the C<U32 wI<n>> parameters are warning category 607constants. You can see the ones currently available in 608L<warnings/Category Hierarchy>, just capitalize all letters in the names 609and prefix them by C<WARN_>. So, for example, the category C<void> used in a 610perl program becomes C<WARN_VOID> when used in XS code and passed to one of 611the calls below. 612 613=for apidoc Am|bool|ckWARN|U32 w 614=for apidoc_item ||ckWARN2|U32 w1|U32 w2 615=for apidoc_item ||ckWARN3|U32 w1|U32 w2|U32 w3 616=for apidoc_item ||ckWARN4|U32 w1|U32 w2|U32 w3|U32 w4 617These return a boolean as to whether or not warnings are enabled for any of 618the warning category(ies) parameters: C<w>, C<w1>, .... 619 620Should any of the categories by default be enabled even if not within the 621scope of S<C<use warnings>>, instead use the C<L</ckWARN_d>> macros. 622 623The categories must be completely independent, one may not be subclassed from 624the other. 625 626=for apidoc Am|bool|ckWARN_d|U32 w 627=for apidoc_item ||ckWARN2_d|U32 w1|U32 w2 628=for apidoc_item ||ckWARN3_d|U32 w1|U32 w2|U32 w3 629=for apidoc_item ||ckWARN4_d|U32 w1|U32 w2|U32 w3|U32 w4 630 631Like C<L</ckWARN>>, but for use if and only if the warning category(ies) is by 632default enabled even if not within the scope of S<C<use warnings>>. 633 634=for apidoc Am|U32|packWARN|U32 w1 635=for apidoc_item ||packWARN2|U32 w1|U32 w2 636=for apidoc_item ||packWARN3|U32 w1|U32 w2|U32 w3 637=for apidoc_item ||packWARN4|U32 w1|U32 w2|U32 w3|U32 w4 638 639These macros are used to pack warning categories into a single U32 to pass to 640macros and functions that take a warning category parameter. The number of 641categories to pack is given by the name, with a corresponding number of 642category parameters passed. 643 644=cut 645 646*/ 647 648#define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w)) 649 650/* The w1, w2 ... should be independent warnings categories; one shouldn't be 651 * a subcategory of any other */ 652 653#define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2)) 654#define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3)) 655#define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4)) 656 657#define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w)) 658#define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2)) 659#define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3)) 660#define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4)) 661 662#define WARNshift 8 663 664#define packWARN(a) (a ) 665 666/* The a, b, ... should be independent warnings categories; one shouldn't be 667 * a subcategory of any other */ 668 669#define packWARN2(a,b) ((a) | ((b)<<8) ) 670#define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) ) 671#define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24)) 672 673#define unpackWARN1(x) ((U8) (x) ) 674#define unpackWARN2(x) ((U8) ((x) >> 8)) 675#define unpackWARN3(x) ((U8) ((x) >> 16)) 676#define unpackWARN4(x) ((U8) ((x) >> 24)) 677 678#define ckDEAD(x) \ 679 (PL_curcop && \ 680 !specialWARN(PL_curcop->cop_warnings) && \ 681 (isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \ 682 (unpackWARN2(x) && \ 683 (isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \ 684 (unpackWARN3(x) && \ 685 (isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \ 686 (unpackWARN4(x) && \ 687 isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))))))) 688 689EOM 690 691# ----------------------------------------------------------------- 692 693__END__ 694package warnings; 695 696VERSION 697 698# Verify that we're called correctly so that warnings will work. 699# Can't use Carp, since Carp uses us! 700# String regexps because constant folding = smaller optree = less memory vs regexp literal 701# see also strict.pm. 702die sprintf "Incorrect use of pragma '%s' at %s line %d.\n", __PACKAGE__, +(caller)[1,2] 703 if __FILE__ !~ ( '(?x) \b '.__PACKAGE__.' \.pmc? \z' ) 704 && __FILE__ =~ ( '(?x) \b (?i:'.__PACKAGE__.') \.pmc? \z' ); 705 706KEYWORDS 707 708sub Croaker 709{ 710 require Carp; # this initializes %CarpInternal 711 local $Carp::CarpInternal{'warnings'}; 712 delete $Carp::CarpInternal{'warnings'}; 713 Carp::croak(@_); 714} 715 716sub _expand_bits { 717 my $bits = shift; 718 my $want_len = ($LAST_BIT + 7) >> 3; 719 my $len = length($bits); 720 if ($len != $want_len) { 721 if ($bits eq "") { 722 $bits = "\x00" x $want_len; 723 } elsif ($len > $want_len) { 724 substr $bits, $want_len, $len-$want_len, ""; 725 } else { 726 my $x = vec($bits, $Offsets{all} >> 1, 2); 727 $x |= $x << 2; 728 $x |= $x << 4; 729 $bits .= chr($x) x ($want_len - $len); 730 } 731 } 732 return $bits; 733} 734 735sub _bits { 736 my $mask = shift ; 737 my $catmask ; 738 my $fatal = 0 ; 739 my $no_fatal = 0 ; 740 741 $mask = _expand_bits($mask); 742 foreach my $word ( @_ ) { 743 next if $NoOp{$word}; 744 if ($word eq 'FATAL') { 745 $fatal = 1; 746 $no_fatal = 0; 747 } 748 elsif ($word eq 'NONFATAL') { 749 $fatal = 0; 750 $no_fatal = 1; 751 } 752 elsif ($catmask = $Bits{$word}) { 753 $mask |= $catmask ; 754 $mask |= $DeadBits{$word} if $fatal ; 755 $mask = ~(~$mask | $DeadBits{$word}) if $no_fatal ; 756 } 757 else 758 { Croaker("Unknown warnings category '$word'")} 759 } 760 761 return $mask ; 762} 763 764sub bits 765{ 766 # called from B::Deparse.pm 767 push @_, 'all' unless @_ ; 768 return _bits("", @_) ; 769} 770 771sub import 772{ 773 my $invocant = shift; 774 775 # append 'all' when implied (empty import list or after a lone 776 # "FATAL" or "NONFATAL") 777 push @_, 'all' 778 if !@_ || (@_==1 && ($_[0] eq 'FATAL' || $_[0] eq 'NONFATAL')); 779 780 my @fatal = (); 781 foreach my $warning (@_) { 782 if($warning =~ /^(NON)?FATAL$/) { 783 @fatal = ($warning); 784 } elsif(substr($warning, 0, 1) ne '-') { 785 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; 786 ${^WARNING_BITS} = _bits($mask, @fatal, $warning); 787 } else { 788 $invocant->unimport(substr($warning, 1)); 789 } 790 } 791} 792 793sub unimport 794{ 795 shift; 796 797 my $catmask ; 798 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; 799 800 # append 'all' when implied (empty import list or after a lone "FATAL") 801 push @_, 'all' if !@_ || @_==1 && $_[0] eq 'FATAL'; 802 803 $mask = _expand_bits($mask); 804 foreach my $word ( @_ ) { 805 next if $NoOp{$word}; 806 if ($word eq 'FATAL') { 807 next; 808 } 809 elsif ($catmask = $Bits{$word}) { 810 $mask = ~(~$mask | $catmask | $DeadBits{$word}); 811 } 812 else 813 { Croaker("Unknown warnings category '$word'")} 814 } 815 816 ${^WARNING_BITS} = $mask ; 817} 818 819my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = (); 820 821sub LEVEL () { 8 }; 822sub MESSAGE () { 4 }; 823sub FATAL () { 2 }; 824sub NORMAL () { 1 }; 825 826sub __chk 827{ 828 my $category ; 829 my $offset ; 830 my $isobj = 0 ; 831 my $wanted = shift; 832 my $has_message = $wanted & MESSAGE; 833 my $has_level = $wanted & LEVEL ; 834 835 if ($has_level) { 836 if (@_ != ($has_message ? 3 : 2)) { 837 my $sub = (caller 1)[3]; 838 my $syntax = $has_message 839 ? "category, level, 'message'" 840 : 'category, level'; 841 Croaker("Usage: $sub($syntax)"); 842 } 843 } 844 elsif (not @_ == 1 || @_ == ($has_message ? 2 : 0)) { 845 my $sub = (caller 1)[3]; 846 my $syntax = $has_message ? "[category,] 'message'" : '[category]'; 847 Croaker("Usage: $sub($syntax)"); 848 } 849 850 my $message = pop if $has_message; 851 852 if (@_) { 853 # check the category supplied. 854 $category = shift ; 855 if (my $type = ref $category) { 856 Croaker("not an object") 857 if exists $builtin_type{$type}; 858 $category = $type; 859 $isobj = 1 ; 860 } 861 $offset = $Offsets{$category}; 862 Croaker("Unknown warnings category '$category'") 863 unless defined $offset; 864 } 865 else { 866 $category = caller(1); 867 $offset = $Offsets{$category}; 868 Croaker("package '$category' not registered for warnings") 869 unless defined $offset ; 870 } 871 872 my $i; 873 874 if ($isobj) { 875 my $pkg; 876 $i = 2; 877 while (do { { package DB; $pkg = (caller($i++))[0] } } ) { 878 last unless @DB::args && $DB::args[0] =~ /^$category=/ ; 879 } 880 $i -= 2 ; 881 } 882 elsif ($has_level) { 883 $i = 2 + shift; 884 } 885 else { 886 $i = _error_loc(); # see where Carp will allocate the error 887 } 888 889 # Default to 0 if caller returns nothing. Default to $DEFAULT if it 890 # explicitly returns undef. 891 my(@callers_bitmask) = (caller($i))[9] ; 892 my $callers_bitmask = 893 @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ; 894 length($callers_bitmask) > ($offset >> 3) or $offset = $Offsets{all}; 895 896 my @results; 897 foreach my $type (FATAL, NORMAL) { 898 next unless $wanted & $type; 899 900 push @results, vec($callers_bitmask, $offset + $type - 1, 1); 901 } 902 903 # &enabled and &fatal_enabled 904 return $results[0] unless $has_message; 905 906 # &warnif, and the category is neither enabled as warning nor as fatal 907 return if ($wanted & (NORMAL | FATAL | MESSAGE)) 908 == (NORMAL | FATAL | MESSAGE) 909 && !($results[0] || $results[1]); 910 911 # If we have an explicit level, bypass Carp. 912 if ($has_level and @callers_bitmask) { 913 # logic copied from util.c:mess_sv 914 my $stuff = " at " . join " line ", (caller $i)[1,2]; 915 $stuff .= sprintf ", <%s> %s %d", 916 *${^LAST_FH}{NAME}, 917 ($/ eq "\n" ? "line" : "chunk"), $. 918 if $. && ${^LAST_FH}; 919 die "$message$stuff.\n" if $results[0]; 920 return warn "$message$stuff.\n"; 921 } 922 923 require Carp; 924 Carp::croak($message) if $results[0]; 925 # will always get here for &warn. will only get here for &warnif if the 926 # category is enabled 927 Carp::carp($message); 928} 929 930sub _mkMask 931{ 932 my ($bit) = @_; 933 my $mask = ""; 934 935 vec($mask, $bit, 1) = 1; 936 return $mask; 937} 938 939sub register_categories 940{ 941 my @names = @_; 942 943 for my $name (@names) { 944 if (! defined $Bits{$name}) { 945 $Offsets{$name} = $LAST_BIT; 946 $Bits{$name} = _mkMask($LAST_BIT++); 947 $DeadBits{$name} = _mkMask($LAST_BIT++); 948 if (length($Bits{$name}) > length($Bits{all})) { 949 $Bits{all} .= "\x55"; 950 $DeadBits{all} .= "\xaa"; 951 } 952 } 953 } 954} 955 956sub _error_loc { 957 require Carp; 958 goto &Carp::short_error_loc; # don't introduce another stack frame 959} 960 961sub enabled 962{ 963 return __chk(NORMAL, @_); 964} 965 966sub fatal_enabled 967{ 968 return __chk(FATAL, @_); 969} 970 971sub warn 972{ 973 return __chk(FATAL | MESSAGE, @_); 974} 975 976sub warnif 977{ 978 return __chk(NORMAL | FATAL | MESSAGE, @_); 979} 980 981sub enabled_at_level 982{ 983 return __chk(NORMAL | LEVEL, @_); 984} 985 986sub fatal_enabled_at_level 987{ 988 return __chk(FATAL | LEVEL, @_); 989} 990 991sub warn_at_level 992{ 993 return __chk(FATAL | MESSAGE | LEVEL, @_); 994} 995 996sub warnif_at_level 997{ 998 return __chk(NORMAL | FATAL | MESSAGE | LEVEL, @_); 999} 1000 1001# These are not part of any public interface, so we can delete them to save 1002# space. 1003delete @warnings::{qw(NORMAL FATAL MESSAGE LEVEL)}; 1004 10051; 1006__END__ 1007 1008=head1 NAME 1009 1010warnings - Perl pragma to control optional warnings 1011 1012=head1 SYNOPSIS 1013 1014 use warnings; 1015 no warnings; 1016 1017 # Standard warnings are enabled by use v5.35 or above 1018 use v5.35; 1019 1020 use warnings "all"; 1021 no warnings "uninitialized"; 1022 1023 # or equivalent to those last two ... 1024 use warnings qw(all -uninitialized); 1025 1026 use warnings::register; 1027 if (warnings::enabled()) { 1028 warnings::warn("some warning"); 1029 } 1030 1031 if (warnings::enabled("void")) { 1032 warnings::warn("void", "some warning"); 1033 } 1034 1035 if (warnings::enabled($object)) { 1036 warnings::warn($object, "some warning"); 1037 } 1038 1039 warnings::warnif("some warning"); 1040 warnings::warnif("void", "some warning"); 1041 warnings::warnif($object, "some warning"); 1042 1043=head1 DESCRIPTION 1044 1045The C<warnings> pragma gives control over which warnings are enabled in 1046which parts of a Perl program. It's a more flexible alternative for 1047both the command line flag B<-w> and the equivalent Perl variable, 1048C<$^W>. 1049 1050This pragma works just like the C<strict> pragma. 1051This means that the scope of the warning pragma is limited to the 1052enclosing block. It also means that the pragma setting will not 1053leak across files (via C<use>, C<require> or C<do>). This allows 1054authors to independently define the degree of warning checks that will 1055be applied to their module. 1056 1057By default, optional warnings are disabled, so any legacy code that 1058doesn't attempt to control the warnings will work unchanged. 1059 1060All warnings are enabled in a block by either of these: 1061 1062 use warnings; 1063 use warnings 'all'; 1064 1065Similarly all warnings are disabled in a block by either of these: 1066 1067 no warnings; 1068 no warnings 'all'; 1069 1070For example, consider the code below: 1071 1072 use warnings; 1073 my @x; 1074 { 1075 no warnings; 1076 my $y = @x[0]; 1077 } 1078 my $z = @x[0]; 1079 1080The code in the enclosing block has warnings enabled, but the inner 1081block has them disabled. In this case that means the assignment to the 1082scalar C<$z> will trip the C<"Scalar value @x[0] better written as $x[0]"> 1083warning, but the assignment to the scalar C<$y> will not. 1084 1085All warnings are enabled automatically within the scope of 1086a C<L<use v5.35|perlfunc/use VERSION>> (or higher) declaration. 1087 1088=head2 Default Warnings and Optional Warnings 1089 1090Before the introduction of lexical warnings, Perl had two classes of 1091warnings: mandatory and optional. 1092 1093As its name suggests, if your code tripped a mandatory warning, you 1094would get a warning whether you wanted it or not. 1095For example, the code below would always produce an C<"isn't numeric"> 1096warning about the "2:". 1097 1098 my $x = "2:" + 3; 1099 1100With the introduction of lexical warnings, mandatory warnings now become 1101I<default> warnings. The difference is that although the previously 1102mandatory warnings are still enabled by default, they can then be 1103subsequently enabled or disabled with the lexical warning pragma. For 1104example, in the code below, an C<"isn't numeric"> warning will only 1105be reported for the C<$x> variable. 1106 1107 my $x = "2:" + 3; 1108 no warnings; 1109 my $y = "2:" + 3; 1110 1111Note that neither the B<-w> flag or the C<$^W> can be used to 1112disable/enable default warnings. They are still mandatory in this case. 1113 1114=head2 "Negative warnings" 1115 1116As a convenience, you can (as of Perl 5.34) pass arguments to the 1117C<import()> method both positively and negatively. Negative warnings 1118are those with a C<-> sign prepended to their names; positive warnings 1119are anything else. This lets you turn on some warnings and turn off 1120others in one command. So, assuming that you've already turned on a 1121bunch of warnings but want to tweak them a bit in some block, you can 1122do this: 1123 1124 { 1125 use warnings qw(uninitialized -redefine); 1126 ... 1127 } 1128 1129which is equivalent to: 1130 1131 { 1132 use warnings qw(uninitialized); 1133 no warnings qw(redefine); 1134 ... 1135 } 1136 1137The argument list is processed in the order you specify. So, for example, if you 1138don't want to be warned about use of experimental features, except for C<somefeature> 1139that you really dislike, you can say this: 1140 1141 use warnings qw(all -experimental experimental::somefeature); 1142 1143which is equivalent to: 1144 1145 use warnings 'all'; 1146 no warnings 'experimental'; 1147 use warnings 'experimental::somefeature'; 1148 1149As experimental features become regular features of Perl, 1150the corresponding warnings are not printed anymore. 1151They also stop being listed in the L</Category Hierarchy> below. 1152 1153It is still possible to request turning on or off these warnings, 1154but doing so has no effect. 1155 1156=head2 What's wrong with B<-w> and C<$^W> 1157 1158Although very useful, the big problem with using B<-w> on the command 1159line to enable warnings is that it is all or nothing. Take the typical 1160scenario when you are writing a Perl program. Parts of the code you 1161will write yourself, but it's very likely that you will make use of 1162pre-written Perl modules. If you use the B<-w> flag in this case, you 1163end up enabling warnings in pieces of code that you haven't written. 1164 1165Similarly, using C<$^W> to either disable or enable blocks of code is 1166fundamentally flawed. For a start, say you want to disable warnings in 1167a block of code. You might expect this to be enough to do the trick: 1168 1169 { 1170 local ($^W) = 0; 1171 my $x =+ 2; 1172 my $y; chop $y; 1173 } 1174 1175When this code is run with the B<-w> flag, a warning will be produced 1176for the C<$x> line: C<"Reversed += operator">. 1177 1178The problem is that Perl has both compile-time and run-time warnings. To 1179disable compile-time warnings you need to rewrite the code like this: 1180 1181 { 1182 BEGIN { $^W = 0 } 1183 my $x =+ 2; 1184 my $y; chop $y; 1185 } 1186 1187And note that unlike the first example, this will permanently set C<$^W> 1188since it cannot both run during compile-time and be localized to a 1189run-time block. 1190 1191The other big problem with C<$^W> is the way you can inadvertently 1192change the warning setting in unexpected places in your code. For example, 1193when the code below is run (without the B<-w> flag), the second call 1194to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas 1195the first will not. 1196 1197 sub doit 1198 { 1199 my $y; chop $y; 1200 } 1201 1202 doit(); 1203 1204 { 1205 local ($^W) = 1; 1206 doit() 1207 } 1208 1209This is a side-effect of C<$^W> being dynamically scoped. 1210 1211Lexical warnings get around these limitations by allowing finer control 1212over where warnings can or can't be tripped. 1213 1214=head2 Controlling Warnings from the Command Line 1215 1216There are three Command Line flags that can be used to control when 1217warnings are (or aren't) produced: 1218 1219=over 5 1220 1221=item B<-w> 1222X<-w> 1223 1224This is the existing flag. If the lexical warnings pragma is B<not> 1225used in any of your code, or any of the modules that you use, this flag 1226will enable warnings everywhere. See L</Backward Compatibility> for 1227details of how this flag interacts with lexical warnings. 1228 1229=item B<-W> 1230X<-W> 1231 1232If the B<-W> flag is used on the command line, it will enable all warnings 1233throughout the program regardless of whether warnings were disabled 1234locally using C<no warnings> or C<$^W =0>. 1235This includes all files that get 1236included via C<use>, C<require> or C<do>. 1237Think of it as the Perl equivalent of the "lint" command. 1238 1239=item B<-X> 1240X<-X> 1241 1242Does the exact opposite to the B<-W> flag, i.e. it disables all warnings. 1243 1244=back 1245 1246=head2 Backward Compatibility 1247 1248If you are used to working with a version of Perl prior to the 1249introduction of lexically scoped warnings, or have code that uses both 1250lexical warnings and C<$^W>, this section will describe how they interact. 1251 1252How Lexical Warnings interact with B<-w>/C<$^W>: 1253 1254=over 5 1255 1256=item 1. 1257 1258If none of the three command line flags (B<-w>, B<-W> or B<-X>) that 1259control warnings is used and neither C<$^W> nor the C<warnings> pragma 1260are used, then default warnings will be enabled and optional warnings 1261disabled. 1262This means that legacy code that doesn't attempt to control the warnings 1263will work unchanged. 1264 1265=item 2. 1266 1267The B<-w> flag just sets the global C<$^W> variable as in 5.005. This 1268means that any legacy code that currently relies on manipulating C<$^W> 1269to control warning behavior will still work as is. 1270 1271=item 3. 1272 1273Apart from now being a boolean, the C<$^W> variable operates in exactly 1274the same horrible uncontrolled global way, except that it cannot 1275disable/enable default warnings. 1276 1277=item 4. 1278 1279If a piece of code is under the control of the C<warnings> pragma, 1280both the C<$^W> variable and the B<-w> flag will be ignored for the 1281scope of the lexical warning. 1282 1283=item 5. 1284 1285The only way to override a lexical warnings setting is with the B<-W> 1286or B<-X> command line flags. 1287 1288=back 1289 1290The combined effect of 3 & 4 is that it will allow code which uses 1291the C<warnings> pragma to control the warning behavior of $^W-type 1292code (using a C<local $^W=0>) if it really wants to, but not vice-versa. 1293 1294=head2 Category Hierarchy 1295X<warning, categories> 1296 1297A hierarchy of "categories" have been defined to allow groups of warnings 1298to be enabled/disabled in isolation. 1299 1300The current hierarchy is: 1301 1302=for warnings.pl tree-goes-here 1303 1304Just like the "strict" pragma any of these categories can be combined 1305 1306 use warnings qw(void redefine); 1307 no warnings qw(io syntax untie); 1308 1309Also like the "strict" pragma, if there is more than one instance of the 1310C<warnings> pragma in a given scope the cumulative effect is additive. 1311 1312 use warnings qw(void); # only "void" warnings enabled 1313 ... 1314 use warnings qw(io); # only "void" & "io" warnings enabled 1315 ... 1316 no warnings qw(void); # only "io" warnings enabled 1317 1318To determine which category a specific warning has been assigned to see 1319L<perldiag>. 1320 1321Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a 1322sub-category of the "syntax" category. It is now a top-level category 1323in its own right. 1324 1325Note: Before 5.21.0, the "missing" lexical warnings category was 1326internally defined to be the same as the "uninitialized" category. It 1327is now a top-level category in its own right. 1328 1329=head2 Fatal Warnings 1330X<warning, fatal> 1331 1332The presence of the word "FATAL" in the category list will escalate 1333warnings in those categories into fatal errors in that lexical scope. 1334 1335B<NOTE:> FATAL warnings should be used with care, particularly 1336C<< FATAL => 'all' >>. 1337 1338Libraries using L<warnings::warn|/FUNCTIONS> for custom warning categories 1339generally don't expect L<warnings::warn|/FUNCTIONS> to be fatal and can wind up 1340in an unexpected state as a result. For XS modules issuing categorized 1341warnings, such unanticipated exceptions could also expose memory leak bugs. 1342 1343Moreover, the Perl interpreter itself has had serious bugs involving 1344fatalized warnings. For a summary of resolved and unresolved problems as 1345of January 2015, please see 1346L<this perl5-porters post|http://www.nntp.perl.org/group/perl.perl5.porters/2015/01/msg225235.html>. 1347 1348While some developers find fatalizing some warnings to be a useful 1349defensive programming technique, using C<< FATAL => 'all' >> to fatalize 1350all possible warning categories -- including custom ones -- is particularly 1351risky. Therefore, the use of C<< FATAL => 'all' >> is 1352L<discouraged|perlpolicy/discouraged>. 1353 1354The L<strictures|strictures/VERSION-2> module on CPAN offers one example of 1355a warnings subset that the module's authors believe is relatively safe to 1356fatalize. 1357 1358B<NOTE:> Users of FATAL warnings, especially those using 1359C<< FATAL => 'all' >>, should be fully aware that they are risking future 1360portability of their programs by doing so. Perl makes absolutely no 1361commitments to not introduce new warnings or warnings categories in the 1362future; indeed, we explicitly reserve the right to do so. Code that may 1363not warn now may warn in a future release of Perl if the Perl5 development 1364team deems it in the best interests of the community to do so. Should code 1365using FATAL warnings break due to the introduction of a new warning we will 1366NOT consider it an incompatible change. Users of FATAL warnings should 1367take special caution during upgrades to check to see if their code triggers 1368any new warnings and should pay particular attention to the fine print of 1369the documentation of the features they use to ensure they do not exploit 1370features that are documented as risky, deprecated, or unspecified, or where 1371the documentation says "so don't do that", or anything with the same sense 1372and spirit. Use of such features in combination with FATAL warnings is 1373ENTIRELY AT THE USER'S RISK. 1374 1375The following documentation describes how to use FATAL warnings but the 1376perl5 porters strongly recommend that you understand the risks before doing 1377so, especially for library code intended for use by others, as there is no 1378way for downstream users to change the choice of fatal categories. 1379 1380In the code below, the use of C<time>, C<length> 1381and C<join> can all produce a C<"Useless use of xxx in void context"> 1382warning. 1383 1384 use warnings; 1385 1386 time; 1387 1388 { 1389 use warnings FATAL => qw(void); 1390 length "abc"; 1391 } 1392 1393 join "", 1,2,3; 1394 1395 print "done\n"; 1396 1397When run it produces this output 1398 1399 Useless use of time in void context at fatal line 3. 1400 Useless use of length in void context at fatal line 7. 1401 1402The scope where C<length> is used has escalated the C<void> warnings 1403category into a fatal error, so the program terminates immediately when it 1404encounters the warning. 1405 1406To explicitly turn off a "FATAL" warning you just disable the warning 1407it is associated with. So, for example, to disable the "void" warning 1408in the example above, either of these will do the trick: 1409 1410 no warnings qw(void); 1411 no warnings FATAL => qw(void); 1412 1413If you want to downgrade a warning that has been escalated into a fatal 1414error back to a normal warning, you can use the "NONFATAL" keyword. For 1415example, the code below will promote all warnings into fatal errors, 1416except for those in the "syntax" category. 1417 1418 use warnings FATAL => 'all', NONFATAL => 'syntax'; 1419 1420As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can 1421use: 1422 1423 use v5.20; # Perl 5.20 or greater is required for the following 1424 use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';" 1425 1426However, you should still heed the guidance earlier in this section against 1427using C<< use warnings FATAL => 'all'; >>. 1428 1429If you want your program to be compatible with versions of Perl before 14305.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In 1431previous versions of Perl, the behavior of the statements 1432C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and 1433C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if 1434they included the C<< => 'all' >> portion. As of 5.20, they do.) 1435 1436=head2 Reporting Warnings from a Module 1437X<warning, reporting> X<warning, registering> 1438 1439The C<warnings> pragma provides a number of functions that are useful for 1440module authors. These are used when you want to report a module-specific 1441warning to a calling module has enabled warnings via the C<warnings> 1442pragma. 1443 1444Consider the module C<MyMod::Abc> below. 1445 1446 package MyMod::Abc; 1447 1448 use warnings::register; 1449 1450 sub open { 1451 my $path = shift; 1452 if ($path !~ m#^/#) { 1453 warnings::warn("changing relative path to /var/abc") 1454 if warnings::enabled(); 1455 $path = "/var/abc/$path"; 1456 } 1457 } 1458 1459 1; 1460 1461The call to C<warnings::register> will create a new warnings category 1462called "MyMod::Abc", i.e. the new category name matches the current 1463package name. The C<open> function in the module will display a warning 1464message if it gets given a relative path as a parameter. This warnings 1465will only be displayed if the code that uses C<MyMod::Abc> has actually 1466enabled them with the C<warnings> pragma like below. 1467 1468 use MyMod::Abc; 1469 use warnings 'MyMod::Abc'; 1470 ... 1471 abc::open("../fred.txt"); 1472 1473It is also possible to test whether the pre-defined warnings categories are 1474set in the calling module with the C<warnings::enabled> function. Consider 1475this snippet of code: 1476 1477 package MyMod::Abc; 1478 1479 sub open { 1480 if (warnings::enabled("deprecated")) { 1481 warnings::warn("deprecated", 1482 "open is deprecated, use new instead"); 1483 } 1484 new(@_); 1485 } 1486 1487 sub new 1488 ... 1489 1; 1490 1491The function C<open> has been deprecated, so code has been included to 1492display a warning message whenever the calling module has (at least) the 1493"deprecated" warnings category enabled. Something like this, say. 1494 1495 use warnings 'deprecated'; 1496 use MyMod::Abc; 1497 ... 1498 MyMod::Abc::open($filename); 1499 1500Either the C<warnings::warn> or C<warnings::warnif> function should be 1501used to actually display the warnings message. This is because they can 1502make use of the feature that allows warnings to be escalated into fatal 1503errors. So in this case 1504 1505 use MyMod::Abc; 1506 use warnings FATAL => 'MyMod::Abc'; 1507 ... 1508 MyMod::Abc::open('../fred.txt'); 1509 1510the C<warnings::warnif> function will detect this and die after 1511displaying the warning message. 1512 1513The three warnings functions, C<warnings::warn>, C<warnings::warnif> 1514and C<warnings::enabled> can optionally take an object reference in place 1515of a category name. In this case the functions will use the class name 1516of the object as the warnings category. 1517 1518Consider this example: 1519 1520 package Original; 1521 1522 no warnings; 1523 use warnings::register; 1524 1525 sub new 1526 { 1527 my $class = shift; 1528 bless [], $class; 1529 } 1530 1531 sub check 1532 { 1533 my $self = shift; 1534 my $value = shift; 1535 1536 if ($value % 2 && warnings::enabled($self)) 1537 { warnings::warn($self, "Odd numbers are unsafe") } 1538 } 1539 1540 sub doit 1541 { 1542 my $self = shift; 1543 my $value = shift; 1544 $self->check($value); 1545 # ... 1546 } 1547 1548 1; 1549 1550 package Derived; 1551 1552 use warnings::register; 1553 use Original; 1554 our @ISA = qw( Original ); 1555 sub new 1556 { 1557 my $class = shift; 1558 bless [], $class; 1559 } 1560 1561 1562 1; 1563 1564The code below makes use of both modules, but it only enables warnings from 1565C<Derived>. 1566 1567 use Original; 1568 use Derived; 1569 use warnings 'Derived'; 1570 my $x = Original->new(); 1571 $x->doit(1); 1572 my $y = Derived->new(); 1573 $x->doit(1); 1574 1575When this code is run only the C<Derived> object, C<$y>, will generate 1576a warning. 1577 1578 Odd numbers are unsafe at main.pl line 7 1579 1580Notice also that the warning is reported at the line where the object is first 1581used. 1582 1583When registering new categories of warning, you can supply more names to 1584warnings::register like this: 1585 1586 package MyModule; 1587 use warnings::register qw(format precision); 1588 1589 ... 1590 1591 warnings::warnif('MyModule::format', '...'); 1592 1593=head1 FUNCTIONS 1594 1595Note: The functions with names ending in C<_at_level> were added in Perl 15965.28. 1597 1598=over 4 1599 1600=item use warnings::register 1601 1602Creates a new warnings category with the same name as the package where 1603the call to the pragma is used. 1604 1605=item warnings::enabled() 1606 1607Use the warnings category with the same name as the current package. 1608 1609Return TRUE if that warnings category is enabled in the calling module. 1610Otherwise returns FALSE. 1611 1612=item warnings::enabled($category) 1613 1614Return TRUE if the warnings category, C<$category>, is enabled in the 1615calling module. 1616Otherwise returns FALSE. 1617 1618=item warnings::enabled($object) 1619 1620Use the name of the class for the object reference, C<$object>, as the 1621warnings category. 1622 1623Return TRUE if that warnings category is enabled in the first scope 1624where the object is used. 1625Otherwise returns FALSE. 1626 1627=item warnings::enabled_at_level($category, $level) 1628 1629Like C<warnings::enabled>, but $level specifies the exact call frame, 0 1630being the immediate caller. 1631 1632=item warnings::fatal_enabled() 1633 1634Return TRUE if the warnings category with the same name as the current 1635package has been set to FATAL in the calling module. 1636Otherwise returns FALSE. 1637 1638=item warnings::fatal_enabled($category) 1639 1640Return TRUE if the warnings category C<$category> has been set to FATAL in 1641the calling module. 1642Otherwise returns FALSE. 1643 1644=item warnings::fatal_enabled($object) 1645 1646Use the name of the class for the object reference, C<$object>, as the 1647warnings category. 1648 1649Return TRUE if that warnings category has been set to FATAL in the first 1650scope where the object is used. 1651Otherwise returns FALSE. 1652 1653=item warnings::fatal_enabled_at_level($category, $level) 1654 1655Like C<warnings::fatal_enabled>, but $level specifies the exact call frame, 16560 being the immediate caller. 1657 1658=item warnings::warn($message) 1659 1660Print C<$message> to STDERR. 1661 1662Use the warnings category with the same name as the current package. 1663 1664If that warnings category has been set to "FATAL" in the calling module 1665then die. Otherwise return. 1666 1667=item warnings::warn($category, $message) 1668 1669Print C<$message> to STDERR. 1670 1671If the warnings category, C<$category>, has been set to "FATAL" in the 1672calling module then die. Otherwise return. 1673 1674=item warnings::warn($object, $message) 1675 1676Print C<$message> to STDERR. 1677 1678Use the name of the class for the object reference, C<$object>, as the 1679warnings category. 1680 1681If that warnings category has been set to "FATAL" in the scope where C<$object> 1682is first used then die. Otherwise return. 1683 1684=item warnings::warn_at_level($category, $level, $message) 1685 1686Like C<warnings::warn>, but $level specifies the exact call frame, 16870 being the immediate caller. 1688 1689=item warnings::warnif($message) 1690 1691Equivalent to: 1692 1693 if (warnings::enabled()) 1694 { warnings::warn($message) } 1695 1696=item warnings::warnif($category, $message) 1697 1698Equivalent to: 1699 1700 if (warnings::enabled($category)) 1701 { warnings::warn($category, $message) } 1702 1703=item warnings::warnif($object, $message) 1704 1705Equivalent to: 1706 1707 if (warnings::enabled($object)) 1708 { warnings::warn($object, $message) } 1709 1710=item warnings::warnif_at_level($category, $level, $message) 1711 1712Like C<warnings::warnif>, but $level specifies the exact call frame, 17130 being the immediate caller. 1714 1715=item warnings::register_categories(@names) 1716 1717This registers warning categories for the given names and is primarily for 1718use by the warnings::register pragma. 1719 1720=back 1721 1722See also L<perlmodlib/Pragmatic Modules> and L<perldiag>. 1723 1724=cut 1725