summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Signes <rjbs@cpan.org>2014-03-18 13:29:54 -0400
committerRicardo Signes <rjbs@cpan.org>2014-03-18 13:29:54 -0400
commitabda4963ba7186d442f9f005c2a04308f0e13418 (patch)
treeac246184ba2c7bd07138e1fd5ae04800bfec6125
parent4e75700d82cf54133ceb54bd6ea7b4e136b99beb (diff)
parenta7f2b7af293abc1754863e50deb95f196276d197 (diff)
downloadperl-abda4963ba7186d442f9f005c2a04308f0e13418.tar.gz
Merge perllexwarn.pod into warnings.pm
-rw-r--r--Porting/todo.pod2
-rw-r--r--ext/File-Find/lib/File/Find.pm4
-rw-r--r--lib/B/Deparse.pm2
-rw-r--r--lib/warnings.pm600
-rw-r--r--lib/warnings/register.pm5
-rw-r--r--pod/perldiag.pod2
-rw-r--r--pod/perlfunc.pod2
-rw-r--r--pod/perllexwarn.pod599
-rw-r--r--pod/perlrun.pod6
-rw-r--r--pod/perltrap.pod2
-rw-r--r--pod/perlunicode.pod2
-rw-r--r--regen/warnings.pl608
-rw-r--r--t/porting/podcheck.t1
-rw-r--r--t/porting/regen.t2
14 files changed, 1131 insertions, 706 deletions
diff --git a/Porting/todo.pod b/Porting/todo.pod
index b3aff0943f..e890236d43 100644
--- a/Porting/todo.pod
+++ b/Porting/todo.pod
@@ -856,7 +856,7 @@ if available-- but B<only> if available, all platforms will B<not>
have catgets().
For the really pure at heart, consider extending this item to cover
-also the warning messages (see L<perllexwarn>, C<warnings.pl>).
+also the warning messages (see L<warnings>, C<regen/warnings.pl>).
=head1 Tasks that need a knowledge of the interpreter
diff --git a/ext/File-Find/lib/File/Find.pm b/ext/File-Find/lib/File/Find.pm
index 59953129cb..6cfdb59aef 100644
--- a/ext/File-Find/lib/File/Find.pm
+++ b/ext/File-Find/lib/File/Find.pm
@@ -3,7 +3,7 @@ use 5.006;
use strict;
use warnings;
use warnings::register;
-our $VERSION = '1.26';
+our $VERSION = '1.27';
require Exporter;
require Cwd;
@@ -299,7 +299,7 @@ situations. You can disable these warnings by putting the statement
no warnings 'File::Find';
-in the appropriate scope. See L<perllexwarn> for more info about lexical
+in the appropriate scope. See L<warnings> for more info about lexical
warnings.
=head1 CAVEAT
diff --git a/lib/B/Deparse.pm b/lib/B/Deparse.pm
index 80c6401844..e7eb8cf90d 100644
--- a/lib/B/Deparse.pm
+++ b/lib/B/Deparse.pm
@@ -5336,7 +5336,7 @@ parameter twice:
warnings => [FATAL => qw/void io/],
);
-See L<perllexwarn> for more information about lexical warnings.
+See L<warnings> for more information about lexical warnings.
=item hint_bits
diff --git a/lib/warnings.pm b/lib/warnings.pm
index 38f9ce25f1..059c7c5b24 100644
--- a/lib/warnings.pm
+++ b/lib/warnings.pm
@@ -5,7 +5,7 @@
package warnings;
-our $VERSION = '1.22';
+our $VERSION = '1.23';
# Verify that we're called correctly so that warnings will work.
# see also strict.pm.
@@ -45,15 +45,587 @@ warnings - Perl pragma to control optional warnings
=head1 DESCRIPTION
-The C<warnings> pragma is a replacement for the command line flag C<-w>,
-but the pragma is limited to the enclosing block, while the flag is global.
-See L<perllexwarn> for more information and the list of built-in warning
-categories.
+The C<warnings> pragma gives control over which warnings are enabled in
+which parts of a Perl program. It's a more flexible alternative for
+both the command line flag B<-w> and the equivalent Perl variable,
+C<$^W>.
-If no import list is supplied, all possible warnings are either enabled
-or disabled.
+This pragma works just like the C<strict> pragma.
+This means that the scope of the warning pragma is limited to the
+enclosing block. It also means that the pragma setting will not
+leak across files (via C<use>, C<require> or C<do>). This allows
+authors to independently define the degree of warning checks that will
+be applied to their module.
-A number of functions are provided to assist module authors.
+By default, optional warnings are disabled, so any legacy code that
+doesn't attempt to control the warnings will work unchanged.
+
+All warnings are enabled in a block by either of these:
+
+ use warnings;
+ use warnings 'all';
+
+Similarly all warnings are disabled in a block by either of these:
+
+ no warnings;
+ no warnings 'all';
+
+For example, consider the code below:
+
+ use warnings;
+ my @a;
+ {
+ no warnings;
+ my $b = @a[0];
+ }
+ my $c = @a[0];
+
+The code in the enclosing block has warnings enabled, but the inner
+block has them disabled. In this case that means the assignment to the
+scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
+warning, but the assignment to the scalar C<$b> will not.
+
+=head2 Default Warnings and Optional Warnings
+
+Before the introduction of lexical warnings, Perl had two classes of
+warnings: mandatory and optional.
+
+As its name suggests, if your code tripped a mandatory warning, you
+would get a warning whether you wanted it or not.
+For example, the code below would always produce an C<"isn't numeric">
+warning about the "2:".
+
+ my $a = "2:" + 3;
+
+With the introduction of lexical warnings, mandatory warnings now become
+I<default> warnings. The difference is that although the previously
+mandatory warnings are still enabled by default, they can then be
+subsequently enabled or disabled with the lexical warning pragma. For
+example, in the code below, an C<"isn't numeric"> warning will only
+be reported for the C<$a> variable.
+
+ my $a = "2:" + 3;
+ no warnings;
+ my $b = "2:" + 3;
+
+Note that neither the B<-w> flag or the C<$^W> can be used to
+disable/enable default warnings. They are still mandatory in this case.
+
+=head2 What's wrong with B<-w> and C<$^W>
+
+Although very useful, the big problem with using B<-w> on the command
+line to enable warnings is that it is all or nothing. Take the typical
+scenario when you are writing a Perl program. Parts of the code you
+will write yourself, but it's very likely that you will make use of
+pre-written Perl modules. If you use the B<-w> flag in this case, you
+end up enabling warnings in pieces of code that you haven't written.
+
+Similarly, using C<$^W> to either disable or enable blocks of code is
+fundamentally flawed. For a start, say you want to disable warnings in
+a block of code. You might expect this to be enough to do the trick:
+
+ {
+ local ($^W) = 0;
+ my $a =+ 2;
+ my $b; chop $b;
+ }
+
+When this code is run with the B<-w> flag, a warning will be produced
+for the C<$a> line: C<"Reversed += operator">.
+
+The problem is that Perl has both compile-time and run-time warnings. To
+disable compile-time warnings you need to rewrite the code like this:
+
+ {
+ BEGIN { $^W = 0 }
+ my $a =+ 2;
+ my $b; chop $b;
+ }
+
+The other big problem with C<$^W> is the way you can inadvertently
+change the warning setting in unexpected places in your code. For example,
+when the code below is run (without the B<-w> flag), the second call
+to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas
+the first will not.
+
+ sub doit
+ {
+ my $b; chop $b;
+ }
+
+ doit();
+
+ {
+ local ($^W) = 1;
+ doit()
+ }
+
+This is a side-effect of C<$^W> being dynamically scoped.
+
+Lexical warnings get around these limitations by allowing finer control
+over where warnings can or can't be tripped.
+
+=head2 Controlling Warnings from the Command Line
+
+There are three Command Line flags that can be used to control when
+warnings are (or aren't) produced:
+
+=over 5
+
+=item B<-w>
+X<-w>
+
+This is the existing flag. If the lexical warnings pragma is B<not>
+used in any of you code, or any of the modules that you use, this flag
+will enable warnings everywhere. See L<Backward Compatibility> for
+details of how this flag interacts with lexical warnings.
+
+=item B<-W>
+X<-W>
+
+If the B<-W> flag is used on the command line, it will enable all warnings
+throughout the program regardless of whether warnings were disabled
+locally using C<no warnings> or C<$^W =0>.
+This includes all files that get
+included via C<use>, C<require> or C<do>.
+Think of it as the Perl equivalent of the "lint" command.
+
+=item B<-X>
+X<-X>
+
+Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
+
+=back
+
+=head2 Backward Compatibility
+
+If you are used to working with a version of Perl prior to the
+introduction of lexically scoped warnings, or have code that uses both
+lexical warnings and C<$^W>, this section will describe how they interact.
+
+How Lexical Warnings interact with B<-w>/C<$^W>:
+
+=over 5
+
+=item 1.
+
+If none of the three command line flags (B<-w>, B<-W> or B<-X>) that
+control warnings is used and neither C<$^W> nor the C<warnings> pragma
+are used, then default warnings will be enabled and optional warnings
+disabled.
+This means that legacy code that doesn't attempt to control the warnings
+will work unchanged.
+
+=item 2.
+
+The B<-w> flag just sets the global C<$^W> variable as in 5.005. This
+means that any legacy code that currently relies on manipulating C<$^W>
+to control warning behavior will still work as is.
+
+=item 3.
+
+Apart from now being a boolean, the C<$^W> variable operates in exactly
+the same horrible uncontrolled global way, except that it cannot
+disable/enable default warnings.
+
+=item 4.
+
+If a piece of code is under the control of the C<warnings> pragma,
+both the C<$^W> variable and the B<-w> flag will be ignored for the
+scope of the lexical warning.
+
+=item 5.
+
+The only way to override a lexical warnings setting is with the B<-W>
+or B<-X> command line flags.
+
+=back
+
+The combined effect of 3 & 4 is that it will allow code which uses
+the C<warnings> pragma to control the warning behavior of $^W-type
+code (using a C<local $^W=0>) if it really wants to, but not vice-versa.
+
+=head2 Category Hierarchy
+X<warning, categories>
+
+A hierarchy of "categories" have been defined to allow groups of warnings
+to be enabled/disabled in isolation.
+
+The current hierarchy is:
+
+ all -+
+ |
+ +- closure
+ |
+ +- deprecated
+ |
+ +- exiting
+ |
+ +- experimental --+
+ | |
+ | +- experimental::autoderef
+ | |
+ | +- experimental::lexical_subs
+ | |
+ | +- experimental::lexical_topic
+ | |
+ | +- experimental::postderef
+ | |
+ | +- experimental::regex_sets
+ | |
+ | +- experimental::signatures
+ | |
+ | +- experimental::smartmatch
+ |
+ +- glob
+ |
+ +- imprecision
+ |
+ +- io ------------+
+ | |
+ | +- closed
+ | |
+ | +- exec
+ | |
+ | +- layer
+ | |
+ | +- newline
+ | |
+ | +- pipe
+ | |
+ | +- syscalls
+ | |
+ | +- unopened
+ |
+ +- misc
+ |
+ +- numeric
+ |
+ +- once
+ |
+ +- overflow
+ |
+ +- pack
+ |
+ +- portable
+ |
+ +- recursion
+ |
+ +- redefine
+ |
+ +- regexp
+ |
+ +- severe --------+
+ | |
+ | +- debugging
+ | |
+ | +- inplace
+ | |
+ | +- internal
+ | |
+ | +- malloc
+ |
+ +- signal
+ |
+ +- substr
+ |
+ +- syntax --------+
+ | |
+ | +- ambiguous
+ | |
+ | +- bareword
+ | |
+ | +- digit
+ | |
+ | +- illegalproto
+ | |
+ | +- parenthesis
+ | |
+ | +- precedence
+ | |
+ | +- printf
+ | |
+ | +- prototype
+ | |
+ | +- qw
+ | |
+ | +- reserved
+ | |
+ | +- semicolon
+ |
+ +- taint
+ |
+ +- threads
+ |
+ +- uninitialized
+ |
+ +- unpack
+ |
+ +- untie
+ |
+ +- utf8 ----------+
+ | |
+ | +- non_unicode
+ | |
+ | +- nonchar
+ | |
+ | +- surrogate
+ |
+ +- void
+
+Just like the "strict" pragma any of these categories can be combined
+
+ use warnings qw(void redefine);
+ no warnings qw(io syntax untie);
+
+Also like the "strict" pragma, if there is more than one instance of the
+C<warnings> pragma in a given scope the cumulative effect is additive.
+
+ use warnings qw(void); # only "void" warnings enabled
+ ...
+ use warnings qw(io); # only "void" & "io" warnings enabled
+ ...
+ no warnings qw(void); # only "io" warnings enabled
+
+To determine which category a specific warning has been assigned to see
+L<perldiag>.
+
+Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a
+sub-category of the "syntax" category. It is now a top-level category
+in its own right.
+
+=head2 Fatal Warnings
+X<warning, fatal>
+
+The presence of the word "FATAL" in the category list will escalate any
+warnings detected from the categories specified in the lexical scope
+into fatal errors. In the code below, the use of C<time>, C<length>
+and C<join> can all produce a C<"Useless use of xxx in void context">
+warning.
+
+ use warnings;
+
+ time;
+
+ {
+ use warnings FATAL => qw(void);
+ length "abc";
+ }
+
+ join "", 1,2,3;
+
+ print "done\n";
+
+When run it produces this output
+
+ Useless use of time in void context at fatal line 3.
+ Useless use of length in void context at fatal line 7.
+
+The scope where C<length> is used has escalated the C<void> warnings
+category into a fatal error, so the program terminates immediately when it
+encounters the warning.
+
+To explicitly turn off a "FATAL" warning you just disable the warning
+it is associated with. So, for example, to disable the "void" warning
+in the example above, either of these will do the trick:
+
+ no warnings qw(void);
+ no warnings FATAL => qw(void);
+
+If you want to downgrade a warning that has been escalated into a fatal
+error back to a normal warning, you can use the "NONFATAL" keyword. For
+example, the code below will promote all warnings into fatal errors,
+except for those in the "syntax" category.
+
+ use warnings FATAL => 'all', NONFATAL => 'syntax';
+
+As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can
+use:
+
+ use v5.20; # Perl 5.20 or greater is required for the following
+ use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';"
+
+If you want your program to be compatible with versions of Perl before
+5.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In
+previous versions of Perl, the behavior of the statements
+C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and
+C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if
+they included the C<< => 'all' >> portion. As of 5.20, they do.)
+
+B<NOTE:> Users of FATAL warnings, especially
+those using C<< FATAL => 'all' >>
+should be fully aware that they are risking future portability of their
+programs by doing so. Perl makes absolutely no commitments to not
+introduce new warnings, or warnings categories in the future, and indeed
+we explicitly reserve the right to do so. Code that may not warn now may
+warn in a future release of Perl if the Perl5 development team deems it
+in the best interests of the community to do so. Should code using FATAL
+warnings break due to the introduction of a new warning we will NOT
+consider it an incompatible change. Users of FATAL warnings should take
+special caution during upgrades to check to see if their code triggers
+any new warnings and should pay particular attention to the fine print of
+the documentation of the features they use to ensure they do not exploit
+features that are documented as risky, deprecated, or unspecified, or where
+the documentation says "so don't do that", or anything with the same sense
+and spirit. Use of such features in combination with FATAL warnings is
+ENTIRELY AT THE USER'S RISK.
+
+=head2 Reporting Warnings from a Module
+X<warning, reporting> X<warning, registering>
+
+The C<warnings> pragma provides a number of functions that are useful for
+module authors. These are used when you want to report a module-specific
+warning to a calling module has enabled warnings via the C<warnings>
+pragma.
+
+Consider the module C<MyMod::Abc> below.
+
+ package MyMod::Abc;
+
+ use warnings::register;
+
+ sub open {
+ my $path = shift;
+ if ($path !~ m#^/#) {
+ warnings::warn("changing relative path to /var/abc")
+ if warnings::enabled();
+ $path = "/var/abc/$path";
+ }
+ }
+
+ 1;
+
+The call to C<warnings::register> will create a new warnings category
+called "MyMod::Abc", i.e. the new category name matches the current
+package name. The C<open> function in the module will display a warning
+message if it gets given a relative path as a parameter. This warnings
+will only be displayed if the code that uses C<MyMod::Abc> has actually
+enabled them with the C<warnings> pragma like below.
+
+ use MyMod::Abc;
+ use warnings 'MyMod::Abc';
+ ...
+ abc::open("../fred.txt");
+
+It is also possible to test whether the pre-defined warnings categories are
+set in the calling module with the C<warnings::enabled> function. Consider
+this snippet of code:
+
+ package MyMod::Abc;
+
+ sub open {
+ warnings::warnif("deprecated",
+ "open is deprecated, use new instead");
+ new(@_);
+ }
+
+ sub new
+ ...
+ 1;
+
+The function C<open> has been deprecated, so code has been included to
+display a warning message whenever the calling module has (at least) the
+"deprecated" warnings category enabled. Something like this, say.
+
+ use warnings 'deprecated';
+ use MyMod::Abc;
+ ...
+ MyMod::Abc::open($filename);
+
+Either the C<warnings::warn> or C<warnings::warnif> function should be
+used to actually display the warnings message. This is because they can
+make use of the feature that allows warnings to be escalated into fatal
+errors. So in this case
+
+ use MyMod::Abc;
+ use warnings FATAL => 'MyMod::Abc';
+ ...
+ MyMod::Abc::open('../fred.txt');
+
+the C<warnings::warnif> function will detect this and die after
+displaying the warning message.
+
+The three warnings functions, C<warnings::warn>, C<warnings::warnif>
+and C<warnings::enabled> can optionally take an object reference in place
+of a category name. In this case the functions will use the class name
+of the object as the warnings category.
+
+Consider this example:
+
+ package Original;
+
+ no warnings;
+ use warnings::register;
+
+ sub new
+ {
+ my $class = shift;
+ bless [], $class;
+ }
+
+ sub check
+ {
+ my $self = shift;
+ my $value = shift;
+
+ if ($value % 2 && warnings::enabled($self))
+ { warnings::warn($self, "Odd numbers are unsafe") }
+ }
+
+ sub doit
+ {
+ my $self = shift;
+ my $value = shift;
+ $self->check($value);
+ # ...
+ }
+
+ 1;
+
+ package Derived;
+
+ use warnings::register;
+ use Original;
+ our @ISA = qw( Original );
+ sub new
+ {
+ my $class = shift;
+ bless [], $class;
+ }
+
+
+ 1;
+
+The code below makes use of both modules, but it only enables warnings from
+C<Derived>.
+
+ use Original;
+ use Derived;
+ use warnings 'Derived';
+ my $a = Original->new();
+ $a->doit(1);
+ my $b = Derived->new();
+ $a->doit(1);
+
+When this code is run only the C<Derived> object, C<$b>, will generate
+a warning.
+
+ Odd numbers are unsafe at main.pl line 7
+
+Notice also that the warning is reported at the line where the object is first
+used.
+
+When registering new categories of warning, you can supply more names to
+warnings::register like this:
+
+ package MyModule;
+ use warnings::register qw(format precision);
+
+ ...
+
+ warnings::warnif('MyModule::format', '...');
+
+=head1 FUNCTIONS
=over 4
@@ -156,11 +728,11 @@ Equivalent to:
=item warnings::register_categories(@names)
This registers warning categories for the given names and is primarily for
-use by the warnings::register pragma, for which see L<perllexwarn>.
+use by the warnings::register pragma.
=back
-See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
+See also L<perlmodlib/Pragmatic Modules> and L<perldiag>.
=cut
@@ -417,7 +989,7 @@ sub bits
return _bits(undef, @_) ;
}
-sub import
+sub import
{
shift;
@@ -430,12 +1002,12 @@ sub import
# append 'all' when implied (after a lone "FATAL" or "NONFATAL")
push @_, 'all' if @_==1 && ( $_[0] eq 'FATAL' || $_[0] eq 'NONFATAL' );
-
+
# Empty @_ is equivalent to @_ = 'all' ;
${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
}
-sub unimport
+sub unimport
{
shift;
@@ -452,7 +1024,7 @@ sub unimport
foreach my $word ( @_ ) {
if ($word eq 'FATAL') {
- next;
+ next;
}
elsif ($catmask = $Bits{$word}) {
$mask &= ~($catmask | $DeadBits{$word} | $All);
diff --git a/lib/warnings/register.pm b/lib/warnings/register.pm
index 45fd9a2fe0..62a3dbfce0 100644
--- a/lib/warnings/register.pm
+++ b/lib/warnings/register.pm
@@ -1,6 +1,6 @@
package warnings::register;
-our $VERSION = '1.02';
+our $VERSION = '1.03';
=pod
@@ -16,8 +16,7 @@ warnings::register - warnings import function
Creates a warnings category with the same name as the current package.
-See L<warnings> and L<perllexwarn> for more information on this module's
-usage.
+See L<warnings> for more information on this module's usage.
=cut
diff --git a/pod/perldiag.pod b/pod/perldiag.pod
index 5482684ddf..09281ee6c8 100644
--- a/pod/perldiag.pod
+++ b/pod/perldiag.pod
@@ -6739,6 +6739,6 @@ shows whereabouts in the regular expression the problem was discovered.
=head1 SEE ALSO
-L<warnings>, L<perllexwarn>, L<diagnostics>.
+L<warnings>, L<diagnostics>.
=cut
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 8f4df97762..b8af8ff885 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -1833,7 +1833,7 @@ C<$@>. Beware that using C<eval> neither silences Perl from printing
warnings to STDERR, nor does it stuff the text of warning messages into C<$@>.
To do either of those, you have to use the C<$SIG{__WARN__}> facility, or
turn off warnings inside the BLOCK or EXPR using S<C<no warnings 'all'>>.
-See L</warn>, L<perlvar>, L<warnings> and L<perllexwarn>.
+See L</warn>, L<perlvar>, and L<warnings>.
Note that, because C<eval> traps otherwise-fatal errors, it is useful for
determining whether a particular feature (such as C<socket> or C<symlink>)
diff --git a/pod/perllexwarn.pod b/pod/perllexwarn.pod
index de980201f4..d60462458b 100644
--- a/pod/perllexwarn.pod
+++ b/pod/perllexwarn.pod
@@ -1,602 +1,11 @@
=head1 NAME
-X<warning, lexical> X<warnings> X<warning>
perllexwarn - Perl Lexical Warnings
=head1 DESCRIPTION
-The C<use warnings> pragma enables to control precisely what warnings are
-to be enabled in which parts of a Perl program. It's a more flexible
-alternative for both the command line flag B<-w> and the equivalent Perl
-variable, C<$^W>.
+Perl v5.6.0 introduced lexical control over the handling of warnings by
+category. The C<warnings> pragma generally replaces the command line flag
+B<-w>. Documentation on the use of lexical warnings, once partly found in
+this document, is now found in the L<warnings> documentation.
-This pragma works just like the C<strict> pragma.
-This means that the scope of the warning pragma is limited to the
-enclosing block. It also means that the pragma setting will not
-leak across files (via C<use>, C<require> or C<do>). This allows
-authors to independently define the degree of warning checks that will
-be applied to their module.
-
-By default, optional warnings are disabled, so any legacy code that
-doesn't attempt to control the warnings will work unchanged.
-
-All warnings are enabled in a block by either of these:
-
- use warnings;
- use warnings 'all';
-
-Similarly all warnings are disabled in a block by either of these:
-
- no warnings;
- no warnings 'all';
-
-For example, consider the code below:
-
- use warnings;
- my @a;
- {
- no warnings;
- my $b = @a[0];
- }
- my $c = @a[0];
-
-The code in the enclosing block has warnings enabled, but the inner
-block has them disabled. In this case that means the assignment to the
-scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
-warning, but the assignment to the scalar C<$b> will not.
-
-=head2 Default Warnings and Optional Warnings
-
-Before the introduction of lexical warnings, Perl had two classes of
-warnings: mandatory and optional.
-
-As its name suggests, if your code tripped a mandatory warning, you
-would get a warning whether you wanted it or not.
-For example, the code below would always produce an C<"isn't numeric">
-warning about the "2:".
-
- my $a = "2:" + 3;
-
-With the introduction of lexical warnings, mandatory warnings now become
-I<default> warnings. The difference is that although the previously
-mandatory warnings are still enabled by default, they can then be
-subsequently enabled or disabled with the lexical warning pragma. For
-example, in the code below, an C<"isn't numeric"> warning will only
-be reported for the C<$a> variable.
-
- my $a = "2:" + 3;
- no warnings;
- my $b = "2:" + 3;
-
-Note that neither the B<-w> flag or the C<$^W> can be used to
-disable/enable default warnings. They are still mandatory in this case.
-
-=head2 What's wrong with B<-w> and C<$^W>
-
-Although very useful, the big problem with using B<-w> on the command
-line to enable warnings is that it is all or nothing. Take the typical
-scenario when you are writing a Perl program. Parts of the code you
-will write yourself, but it's very likely that you will make use of
-pre-written Perl modules. If you use the B<-w> flag in this case, you
-end up enabling warnings in pieces of code that you haven't written.
-
-Similarly, using C<$^W> to either disable or enable blocks of code is
-fundamentally flawed. For a start, say you want to disable warnings in
-a block of code. You might expect this to be enough to do the trick:
-
- {
- local ($^W) = 0;
- my $a =+ 2;
- my $b; chop $b;
- }
-
-When this code is run with the B<-w> flag, a warning will be produced
-for the C<$a> line: C<"Reversed += operator">.
-
-The problem is that Perl has both compile-time and run-time warnings. To
-disable compile-time warnings you need to rewrite the code like this:
-
- {
- BEGIN { $^W = 0 }
- my $a =+ 2;
- my $b; chop $b;
- }
-
-The other big problem with C<$^W> is the way you can inadvertently
-change the warning setting in unexpected places in your code. For example,
-when the code below is run (without the B<-w> flag), the second call
-to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas
-the first will not.
-
- sub doit
- {
- my $b; chop $b;
- }
-
- doit();
-
- {
- local ($^W) = 1;
- doit()
- }
-
-This is a side-effect of C<$^W> being dynamically scoped.
-
-Lexical warnings get around these limitations by allowing finer control
-over where warnings can or can't be tripped.
-
-=head2 Controlling Warnings from the Command Line
-
-There are three Command Line flags that can be used to control when
-warnings are (or aren't) produced:
-
-=over 5
-
-=item B<-w>
-X<-w>
-
-This is the existing flag. If the lexical warnings pragma is B<not>
-used in any of you code, or any of the modules that you use, this flag
-will enable warnings everywhere. See L<Backward Compatibility> for
-details of how this flag interacts with lexical warnings.
-
-=item B<-W>
-X<-W>
-
-If the B<-W> flag is used on the command line, it will enable all warnings
-throughout the program regardless of whether warnings were disabled
-locally using C<no warnings> or C<$^W =0>.
-This includes all files that get
-included via C<use>, C<require> or C<do>.
-Think of it as the Perl equivalent of the "lint" command.
-
-=item B<-X>
-X<-X>
-
-Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
-
-=back
-
-=head2 Backward Compatibility
-
-If you are used to working with a version of Perl prior to the
-introduction of lexically scoped warnings, or have code that uses both
-lexical warnings and C<$^W>, this section will describe how they interact.
-
-How Lexical Warnings interact with B<-w>/C<$^W>:
-
-=over 5
-
-=item 1.
-
-If none of the three command line flags (B<-w>, B<-W> or B<-X>) that
-control warnings is used and neither C<$^W> nor the C<warnings> pragma
-are used, then default warnings will be enabled and optional warnings
-disabled.
-This means that legacy code that doesn't attempt to control the warnings
-will work unchanged.
-
-=item 2.
-
-The B<-w> flag just sets the global C<$^W> variable as in 5.005. This
-means that any legacy code that currently relies on manipulating C<$^W>
-to control warning behavior will still work as is.
-
-=item 3.
-
-Apart from now being a boolean, the C<$^W> variable operates in exactly
-the same horrible uncontrolled global way, except that it cannot
-disable/enable default warnings.
-
-=item 4.
-
-If a piece of code is under the control of the C<warnings> pragma,
-both the C<$^W> variable and the B<-w> flag will be ignored for the
-scope of the lexical warning.
-
-=item 5.
-
-The only way to override a lexical warnings setting is with the B<-W>
-or B<-X> command line flags.
-
-=back
-
-The combined effect of 3 & 4 is that it will allow code which uses
-the C<warnings> pragma to control the warning behavior of $^W-type
-code (using a C<local $^W=0>) if it really wants to, but not vice-versa.
-
-=head2 Category Hierarchy
-X<warning, categories>
-
-A hierarchy of "categories" have been defined to allow groups of warnings
-to be enabled/disabled in isolation.
-
-The current hierarchy is:
-
-=for comment
-This tree is generated by regen/warnings.pl. Any changes made here
-will be lost.
-
-=for warnings.pl begin
-
- all -+
- |
- +- closure
- |
- +- deprecated
- |
- +- exiting
- |
- +- experimental --+
- | |
- | +- experimental::autoderef
- | |
- | +- experimental::lexical_subs
- | |
- | +- experimental::lexical_topic
- | |
- | +- experimental::postderef
- | |
- | +- experimental::regex_sets
- | |
- | +- experimental::signatures
- | |
- | +- experimental::smartmatch
- |
- +- glob
- |
- +- imprecision
- |
- +- io ------------+
- | |
- | +- closed
- | |
- | +- exec
- | |
- | +- layer
- | |
- | +- newline
- | |
- | +- pipe
- | |
- | +- syscalls
- | |
- | +- unopened
- |
- +- misc
- |
- +- numeric
- |
- +- once
- |
- +- overflow
- |
- +- pack
- |
- +- portable
- |
- +- recursion
- |
- +- redefine
- |
- +- regexp
- |
- +- severe --------+
- | |
- | +- debugging
- | |
- | +- inplace
- | |
- | +- internal
- | |
- | +- malloc
- |
- +- signal
- |
- +- substr
- |
- +- syntax --------+
- | |
- | +- ambiguous
- | |
- | +- bareword
- | |
- | +- digit
- | |
- | +- illegalproto
- | |
- | +- parenthesis
- | |
- | +- precedence
- | |
- | +- printf
- | |
- | +- prototype
- | |
- | +- qw
- | |
- | +- reserved
- | |
- | +- semicolon
- |
- +- taint
- |
- +- threads
- |
- +- uninitialized
- |
- +- unpack
- |
- +- untie
- |
- +- utf8 ----------+
- | |
- | +- non_unicode
- | |
- | +- nonchar
- | |
- | +- surrogate
- |
- +- void
-
-=for warnings.pl end
-
-Just like the "strict" pragma any of these categories can be combined
-
- use warnings qw(void redefine);
- no warnings qw(io syntax untie);
-
-Also like the "strict" pragma, if there is more than one instance of the
-C<warnings> pragma in a given scope the cumulative effect is additive.
-
- use warnings qw(void); # only "void" warnings enabled
- ...
- use warnings qw(io); # only "void" & "io" warnings enabled
- ...
- no warnings qw(void); # only "io" warnings enabled
-
-To determine which category a specific warning has been assigned to see
-L<perldiag>.
-
-Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a
-sub-category of the "syntax" category. It is now a top-level category
-in its own right.
-
-=head2 Fatal Warnings
-X<warning, fatal>
-
-The presence of the word "FATAL" in the category list will escalate any
-warnings detected from the categories specified in the lexical scope
-into fatal errors. In the code below, the use of C<time>, C<length>
-and C<join> can all produce a C<"Useless use of xxx in void context">
-warning.
-
- use warnings;
-
- time;
-
- {
- use warnings FATAL => qw(void);
- length "abc";
- }
-
- join "", 1,2,3;
-
- print "done\n";
-
-When run it produces this output
-
- Useless use of time in void context at fatal line 3.
- Useless use of length in void context at fatal line 7.
-
-The scope where C<length> is used has escalated the C<void> warnings
-category into a fatal error, so the program terminates immediately when it
-encounters the warning.
-
-To explicitly turn off a "FATAL" warning you just disable the warning
-it is associated with. So, for example, to disable the "void" warning
-in the example above, either of these will do the trick:
-
- no warnings qw(void);
- no warnings FATAL => qw(void);
-
-If you want to downgrade a warning that has been escalated into a fatal
-error back to a normal warning, you can use the "NONFATAL" keyword. For
-example, the code below will promote all warnings into fatal errors,
-except for those in the "syntax" category.
-
- use warnings FATAL => 'all', NONFATAL => 'syntax';
-
-As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can
-use:
-
- use v5.20; # Perl 5.20 or greater is required for the following
- use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';"
-
-If you want your program to be compatible with versions of Perl before
-5.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In
-previous versions of Perl, the behavior of the statements
-C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and
-C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if
-they included the C<< => 'all' >> portion. As of 5.20, they do.)
-
-B<NOTE:> Users of FATAL warnings, especially
-those using C<< FATAL => 'all' >>
-should be fully aware that they are risking future portability of their
-programs by doing so. Perl makes absolutely no commitments to not
-introduce new warnings, or warnings categories in the future, and indeed
-we explicitly reserve the right to do so. Code that may not warn now may
-warn in a future release of Perl if the Perl5 development team deems it
-in the best interests of the community to do so. Should code using FATAL
-warnings break due to the introduction of a new warning we will NOT
-consider it an incompatible change. Users of FATAL warnings should take
-special caution during upgrades to check to see if their code triggers
-any new warnings and should pay particular attention to the fine print of
-the documentation of the features they use to ensure they do not exploit
-features that are documented as risky, deprecated, or unspecified, or where
-the documentation says "so don't do that", or anything with the same sense
-and spirit. Use of such features in combination with FATAL warnings is
-ENTIRELY AT THE USER'S RISK.
-
-=head2 Reporting Warnings from a Module
-X<warning, reporting> X<warning, registering>
-
-The C<warnings> pragma provides a number of functions that are useful for
-module authors. These are used when you want to report a module-specific
-warning to a calling module has enabled warnings via the C<warnings>
-pragma.
-
-Consider the module C<MyMod::Abc> below.
-
- package MyMod::Abc;
-
- use warnings::register;
-
- sub open {
- my $path = shift;
- if ($path !~ m#^/#) {
- warnings::warn("changing relative path to /var/abc")
- if warnings::enabled();
- $path = "/var/abc/$path";
- }
- }
-
- 1;
-
-The call to C<warnings::register> will create a new warnings category
-called "MyMod::Abc", i.e. the new category name matches the current
-package name. The C<open> function in the module will display a warning
-message if it gets given a relative path as a parameter. This warnings
-will only be displayed if the code that uses C<MyMod::Abc> has actually
-enabled them with the C<warnings> pragma like below.
-
- use MyMod::Abc;
- use warnings 'MyMod::Abc';
- ...
- abc::open("../fred.txt");
-
-It is also possible to test whether the pre-defined warnings categories are
-set in the calling module with the C<warnings::enabled> function. Consider
-this snippet of code:
-
- package MyMod::Abc;
-
- sub open {
- warnings::warnif("deprecated",
- "open is deprecated, use new instead");
- new(@_);
- }
-
- sub new
- ...
- 1;
-
-The function C<open> has been deprecated, so code has been included to
-display a warning message whenever the calling module has (at least) the
-"deprecated" warnings category enabled. Something like this, say.
-
- use warnings 'deprecated';
- use MyMod::Abc;
- ...
- MyMod::Abc::open($filename);
-
-Either the C<warnings::warn> or C<warnings::warnif> function should be
-used to actually display the warnings message. This is because they can
-make use of the feature that allows warnings to be escalated into fatal
-errors. So in this case
-
- use MyMod::Abc;
- use warnings FATAL => 'MyMod::Abc';
- ...
- MyMod::Abc::open('../fred.txt');
-
-the C<warnings::warnif> function will detect this and die after
-displaying the warning message.
-
-The three warnings functions, C<warnings::warn>, C<warnings::warnif>
-and C<warnings::enabled> can optionally take an object reference in place
-of a category name. In this case the functions will use the class name
-of the object as the warnings category.
-
-Consider this example:
-
- package Original;
-
- no warnings;
- use warnings::register;
-
- sub new
- {
- my $class = shift;
- bless [], $class;
- }
-
- sub check
- {
- my $self = shift;
- my $value = shift;
-
- if ($value % 2 && warnings::enabled($self))
- { warnings::warn($self, "Odd numbers are unsafe") }
- }
-
- sub doit
- {
- my $self = shift;
- my $value = shift;
- $self->check($value);
- # ...
- }
-
- 1;
-
- package Derived;
-
- use warnings::register;
- use Original;
- our @ISA = qw( Original );
- sub new
- {
- my $class = shift;
- bless [], $class;
- }
-
-
- 1;
-
-The code below makes use of both modules, but it only enables warnings from
-C<Derived>.
-
- use Original;
- use Derived;
- use warnings 'Derived';
- my $a = Original->new();
- $a->doit(1);
- my $b = Derived->new();
- $a->doit(1);
-
-When this code is run only the C<Derived> object, C<$b>, will generate
-a warning.
-
- Odd numbers are unsafe at main.pl line 7
-
-Notice also that the warning is reported at the line where the object is first
-used.
-
-When registering new categories of warning, you can supply more names to
-warnings::register like this:
-
- package MyModule;
- use warnings::register qw(format precision);
-
- ...
-
- warnings::warnif('MyModule::format', '...');
-
-=head1 SEE ALSO
-
-L<warnings>, L<perldiag>.
-
-=head1 AUTHOR
-
-Paul Marquess
diff --git a/pod/perlrun.pod b/pod/perlrun.pod
index b54588171e..c6aa81c6c5 100644
--- a/pod/perlrun.pod
+++ b/pod/perlrun.pod
@@ -920,19 +920,19 @@ can disable or promote into fatal errors specific warnings using
C<__WARN__> hooks, as described in L<perlvar> and L<perlfunc/warn>.
See also L<perldiag> and L<perltrap>. A fine-grained warning
facility is also available if you want to manipulate entire classes
-of warnings; see L<warnings> or L<perllexwarn>.
+of warnings; see L<warnings>.
=item B<-W>
X<-W>
Enables all warnings regardless of C<no warnings> or C<$^W>.
-See L<perllexwarn>.
+See L<warnings>.
=item B<-X>
X<-X>
Disables all warnings regardless of C<use warnings> or C<$^W>.
-See L<perllexwarn>.
+See L<warnings>.
=item B<-x>
X<-x>
diff --git a/pod/perltrap.pod b/pod/perltrap.pod
index 1edb7f60f9..7954b7a59b 100644
--- a/pod/perltrap.pod
+++ b/pod/perltrap.pod
@@ -5,7 +5,7 @@ perltrap - Perl traps for the unwary
=head1 DESCRIPTION
The biggest trap of all is forgetting to C<use warnings> or use the B<-w>
-switch; see L<perllexwarn> and L<perlrun>. The second biggest trap is not
+switch; see L<warnings> and L<perlrun>. The second biggest trap is not
making your entire program runnable under C<use strict>. The third biggest
trap is not reading the list of changes in this version of Perl; see
L<perldelta>.
diff --git a/pod/perlunicode.pod b/pod/perlunicode.pod
index 95db7ac355..06cd938556 100644
--- a/pod/perlunicode.pod
+++ b/pod/perlunicode.pod
@@ -1403,7 +1403,7 @@ the C<"non_unicode"> warning class has been made fatal, say by:
use warnings FATAL => "non_unicode"
-(see L<perllexwarn>). In this mode of operation, Perl will raise the
+(see L<warnings>). In this mode of operation, Perl will raise the
warning for all matches against a non-Unicode code point (not just the
arguable ones), and it skips the optimizations that might cause the
warning to not be output. (It currently still won't warn if the match
diff --git a/regen/warnings.pl b/regen/warnings.pl
index 29033ab53e..0e65a269be 100644
--- a/regen/warnings.pl
+++ b/regen/warnings.pl
@@ -1,15 +1,12 @@
#!/usr/bin/perl
-#
+#
# Regenerate (overwriting only if changed):
#
# lib/warnings.pm
-# pod/perllexwarn.pod
# warnings.h
#
# from information hardcoded into this script (the $tree hash), plus the
-# template for warnings.pm in the DATA section. Only part of
-# pod/perllexwarn.pod (the warnings category hierarchy) is generated,
-# the other parts remaining untouched.
+# template for warnings.pm in the DATA section.
#
# When changing the number of warnings, t/op/caller.t should change to
# correspond with the value of $BYTES in lib/warnings.pm
@@ -19,7 +16,7 @@
#
# This script is normally invoked from regen.pl.
-$VERSION = '1.02_05';
+$VERSION = '1.03';
BEGIN {
require 'regen/regen_lib.pl';
@@ -33,7 +30,7 @@ sub DEFAULT_OFF () { 2 }
my $tree = {
'all' => [ 5.008, {
- 'io' => [ 5.008, {
+ 'io' => [ 5.008, {
'pipe' => [ 5.008, DEFAULT_OFF],
'unopened' => [ 5.008, DEFAULT_OFF],
'closed' => [ 5.008, DEFAULT_OFF],
@@ -42,7 +39,7 @@ my $tree = {
'layer' => [ 5.008, DEFAULT_OFF],
'syscalls' => [ 5.019, DEFAULT_OFF],
}],
- 'syntax' => [ 5.008, {
+ 'syntax' => [ 5.008, {
'ambiguous' => [ 5.008, DEFAULT_OFF],
'semicolon' => [ 5.008, DEFAULT_OFF],
'precedence' => [ 5.008, DEFAULT_OFF],
@@ -55,7 +52,7 @@ my $tree = {
'qw' => [ 5.008, DEFAULT_OFF],
'illegalproto' => [ 5.011, DEFAULT_OFF],
}],
- 'severe' => [ 5.008, {
+ 'severe' => [ 5.008, {
'inplace' => [ 5.008, DEFAULT_ON],
'internal' => [ 5.008, DEFAULT_OFF],
'debugging' => [ 5.008, DEFAULT_ON],
@@ -131,7 +128,7 @@ sub valueWalk
my ($ver, $rest) = @{ $v } ;
push @{ $v_list{$ver} }, $k;
-
+
if (ref $rest)
{ valueWalk ($rest) }
@@ -168,7 +165,7 @@ sub walk
push @{ $list{$k} }, $NameToValue{uc $k} ;
die "Value associated with key '$k' is not an ARRAY reference"
if !ref $v || ref $v ne 'ARRAY' ;
-
+
my ($ver, $rest) = @{ $v } ;
if (ref $rest)
{ push (@{ $list{$k} }, walk ($rest)) }
@@ -202,7 +199,7 @@ sub mkRange
}
###########################################################################
-sub printTree
+sub warningsTree
{
my $tre = shift ;
my $prefix = shift ;
@@ -211,19 +208,21 @@ sub printTree
my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ;
my @keys = sort keys %$tre ;
+ my $rv = '';
+
while ($k = shift @keys) {
$v = $tre->{$k};
die "Value associated with key '$k' is not an ARRAY reference"
if !ref $v || ref $v ne 'ARRAY' ;
-
+
my $offset ;
if ($tre ne $tree) {
- print $prefix . "|\n" ;
- print $prefix . "+- $k" ;
+ $rv .= $prefix . "|\n" ;
+ $rv .= $prefix . "+- $k" ;
$offset = ' ' x ($max + 4) ;
}
else {
- print $prefix . "$k" ;
+ $rv .= $prefix . "$k" ;
$offset = ' ' x ($max + 1) ;
}
@@ -231,13 +230,14 @@ sub printTree
if (ref $rest)
{
my $bar = @keys ? "|" : " ";
- print " -" . "-" x ($max - length $k ) . "+\n" ;
- printTree ($rest, $prefix . $bar . $offset )
+ $rv .= " -" . "-" x ($max - length $k ) . "+\n" ;
+ $rv .= warningsTree ($rest, $prefix . $bar . $offset )
}
else
- { print "\n" }
+ { $rv .= "\n" }
}
+ return $rv;
}
###########################################################################
@@ -279,7 +279,7 @@ sub mkOct
if (@ARGV && $ARGV[0] eq "tree")
{
- printTree($tree, " ") ;
+ print warningsTree($tree, " ") ;
exit ;
}
@@ -287,7 +287,12 @@ my ($warn, $pm) = map {
open_new($_, '>', { by => 'regen/warnings.pl' });
} 'warnings.h', 'lib/warnings.pm';
-print $warn <<'EOM';
+my ($index, $warn_size);
+
+{
+ # generate warnings.h
+
+ print $warn <<'EOM';
#define Off(x) ((x) / 8)
#define Bit(x) (1 << ((x) % 8))
@@ -312,39 +317,38 @@ print $warn <<'EOM';
#define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
EOM
-my $offset = 0 ;
+ my $offset = 0 ;
-valueWalk ($tree) ;
-my $index = orderValues();
+ valueWalk ($tree) ;
+ $index = orderValues();
-die <<EOM if $index > 255 ;
+ die <<EOM if $index > 255 ;
Too many warnings categories -- max is 255
- rewrite packWARN* & unpackWARN* macros
+ rewrite packWARN* & unpackWARN* macros
EOM
-walk ($tree) ;
+ walk ($tree) ;
-$index *= 2 ;
-my $warn_size = int($index / 8) + ($index % 8 != 0) ;
+ $index *= 2 ;
+ $warn_size = int($index / 8) + ($index % 8 != 0) ;
-my $k ;
-my $last_ver = 0;
-foreach $k (sort { $a <=> $b } keys %ValueToName) {
- my ($name, $version) = @{ $ValueToName{$k} };
- print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
- if $last_ver != $version ;
- $name =~ y/:/_/;
- print $warn tab(5, "#define WARN_$name"), " $k\n" ;
- $last_ver = $version ;
-}
-print $warn "\n" ;
+ my $k ;
+ my $last_ver = 0;
+ foreach $k (sort { $a <=> $b } keys %ValueToName) {
+ my ($name, $version) = @{ $ValueToName{$k} };
+ print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
+ if $last_ver != $version ;
+ $name =~ y/:/_/;
+ print $warn tab(5, "#define WARN_$name"), " $k\n" ;
+ $last_ver = $version ;
+ }
+ print $warn "\n" ;
-print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
-#print WARN tab(5, '#define WARN_ALLstring'), '"', ('\377' x $warn_size) , "\"\n" ;
-print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
-print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
+ print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
+ print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
+ print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
-print $warn <<'EOM';
+ print $warn <<'EOM';
#define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD)
#define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD)
@@ -398,14 +402,19 @@ print $warn <<'EOM';
/* end of file warnings.h */
EOM
-read_only_bottom_close_and_rename($warn);
+ read_only_bottom_close_and_rename($warn);
+}
while (<DATA>) {
last if /^KEYWORDS$/ ;
+ if ($_ eq "=for warnings.pl tree-goes-here\n") {
+ print $pm warningsTree($tree, " ");
+ next;
+ }
print $pm $_ ;
}
-$last_ver = 0;
+my $last_ver = 0;
print $pm "our %Offsets = (\n" ;
foreach my $k (sort { $a <=> $b } keys %ValueToName) {
my ($name, $version) = @{ $ValueToName{$k} };
@@ -423,7 +432,7 @@ foreach my $k (sort { $a <=> $b } keys %ValueToName) {
print $pm " );\n\n" ;
print $pm "our %Bits = (\n" ;
-foreach $k (sort keys %list) {
+foreach my $k (sort keys %list) {
my $v = $list{$k} ;
my @list = sort { $a <=> $b } @$v ;
@@ -436,7 +445,7 @@ foreach $k (sort keys %list) {
print $pm " );\n\n" ;
print $pm "our %DeadBits = (\n" ;
-foreach $k (sort keys %list) {
+foreach my $k (sort keys %list) {
my $v = $list{$k} ;
my @list = sort { $a <=> $b } @$v ;
@@ -458,29 +467,10 @@ while (<DATA>) {
read_only_bottom_close_and_rename($pm);
-my $lexwarn = open_new 'pod/perllexwarn.pod', '>';
-open my $oldlexwarn, "pod/perllexwarn.pod"
- or die "$0 cannot open pod/perllexwarn.pod for reading: $!";
-select +(select($lexwarn), do {
- while(<$oldlexwarn>) {
- print;
- last if /=for warnings.pl begin/;
- }
- print "\n";
- printTree($tree, " ") ;
- print "\n";
- while(<$oldlexwarn>) {
- last if /=for warnings.pl end/;
- }
- do { print } while <$oldlexwarn>;
-})[0];
-
-close_and_rename($lexwarn);
-
__END__
package warnings;
-our $VERSION = '1.22';
+our $VERSION = '1.23';
# Verify that we're called correctly so that warnings will work.
# see also strict.pm.
@@ -520,15 +510,469 @@ warnings - Perl pragma to control optional warnings
=head1 DESCRIPTION
-The C<warnings> pragma is a replacement for the command line flag C<-w>,
-but the pragma is limited to the enclosing block, while the flag is global.
-See L<perllexwarn> for more information and the list of built-in warning
-categories.
+The C<warnings> pragma gives control over which warnings are enabled in
+which parts of a Perl program. It's a more flexible alternative for
+both the command line flag B<-w> and the equivalent Perl variable,
+C<$^W>.
+
+This pragma works just like the C<strict> pragma.
+This means that the scope of the warning pragma is limited to the
+enclosing block. It also means that the pragma setting will not
+leak across files (via C<use>, C<require> or C<do>). This allows
+authors to independently define the degree of warning checks that will
+be applied to their module.
+
+By default, optional warnings are disabled, so any legacy code that
+doesn't attempt to control the warnings will work unchanged.
+
+All warnings are enabled in a block by either of these:
+
+ use warnings;
+ use warnings 'all';
+
+Similarly all warnings are disabled in a block by either of these:
+
+ no warnings;
+ no warnings 'all';
+
+For example, consider the code below:
+
+ use warnings;
+ my @a;
+ {
+ no warnings;
+ my $b = @a[0];
+ }
+ my $c = @a[0];
+
+The code in the enclosing block has warnings enabled, but the inner
+block has them disabled. In this case that means the assignment to the
+scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
+warning, but the assignment to the scalar C<$b> will not.
+
+=head2 Default Warnings and Optional Warnings
+
+Before the introduction of lexical warnings, Perl had two classes of
+warnings: mandatory and optional.
+
+As its name suggests, if your code tripped a mandatory warning, you
+would get a warning whether you wanted it or not.
+For example, the code below would always produce an C<"isn't numeric">
+warning about the "2:".
+
+ my $a = "2:" + 3;
+
+With the introduction of lexical warnings, mandatory warnings now become
+I<default> warnings. The difference is that although the previously
+mandatory warnings are still enabled by default, they can then be
+subsequently enabled or disabled with the lexical warning pragma. For
+example, in the code below, an C<"isn't numeric"> warning will only
+be reported for the C<$a> variable.
+
+ my $a = "2:" + 3;
+ no warnings;
+ my $b = "2:" + 3;
+
+Note that neither the B<-w> flag or the C<$^W> can be used to
+disable/enable default warnings. They are still mandatory in this case.
+
+=head2 What's wrong with B<-w> and C<$^W>
+
+Although very useful, the big problem with using B<-w> on the command
+line to enable warnings is that it is all or nothing. Take the typical
+scenario when you are writing a Perl program. Parts of the code you
+will write yourself, but it's very likely that you will make use of
+pre-written Perl modules. If you use the B<-w> flag in this case, you
+end up enabling warnings in pieces of code that you haven't written.
+
+Similarly, using C<$^W> to either disable or enable blocks of code is
+fundamentally flawed. For a start, say you want to disable warnings in
+a block of code. You might expect this to be enough to do the trick:
+
+ {
+ local ($^W) = 0;
+ my $a =+ 2;
+ my $b; chop $b;
+ }
+
+When this code is run with the B<-w> flag, a warning will be produced
+for the C<$a> line: C<"Reversed += operator">.
+
+The problem is that Perl has both compile-time and run-time warnings. To
+disable compile-time warnings you need to rewrite the code like this:
+
+ {
+ BEGIN { $^W = 0 }
+ my $a =+ 2;
+ my $b; chop $b;
+ }
+
+The other big problem with C<$^W> is the way you can inadvertently
+change the warning setting in unexpected places in your code. For example,
+when the code below is run (without the B<-w> flag), the second call
+to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas
+the first will not.
+
+ sub doit
+ {
+ my $b; chop $b;
+ }
+
+ doit();
+
+ {
+ local ($^W) = 1;
+ doit()
+ }
+
+This is a side-effect of C<$^W> being dynamically scoped.
+
+Lexical warnings get around these limitations by allowing finer control
+over where warnings can or can't be tripped.
+
+=head2 Controlling Warnings from the Command Line
+
+There are three Command Line flags that can be used to control when
+warnings are (or aren't) produced:
+
+=over 5
+
+=item B<-w>
+X<-w>
+
+This is the existing flag. If the lexical warnings pragma is B<not>
+used in any of you code, or any of the modules that you use, this flag
+will enable warnings everywhere. See L<Backward Compatibility> for
+details of how this flag interacts with lexical warnings.
+
+=item B<-W>
+X<-W>
+
+If the B<-W> flag is used on the command line, it will enable all warnings
+throughout the program regardless of whether warnings were disabled
+locally using C<no warnings> or C<$^W =0>.
+This includes all files that get
+included via C<use>, C<require> or C<do>.
+Think of it as the Perl equivalent of the "lint" command.
+
+=item B<-X>
+X<-X>
+
+Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
+
+=back
+
+=head2 Backward Compatibility
+
+If you are used to working with a version of Perl prior to the
+introduction of lexically scoped warnings, or have code that uses both
+lexical warnings and C<$^W>, this section will describe how they interact.
+
+How Lexical Warnings interact with B<-w>/C<$^W>:
+
+=over 5
+
+=item 1.
+
+If none of the three command line flags (B<-w>, B<-W> or B<-X>) that
+control warnings is used and neither C<$^W> nor the C<warnings> pragma
+are used, then default warnings will be enabled and optional warnings
+disabled.
+This means that legacy code that doesn't attempt to control the warnings
+will work unchanged.
+
+=item 2.
+
+The B<-w> flag just sets the global C<$^W> variable as in 5.005. This
+means that any legacy code that currently relies on manipulating C<$^W>
+to control warning behavior will still work as is.
+
+=item 3.
+
+Apart from now being a boolean, the C<$^W> variable operates in exactly
+the same horrible uncontrolled global way, except that it cannot
+disable/enable default warnings.
+
+=item 4.
+
+If a piece of code is under the control of the C<warnings> pragma,
+both the C<$^W> variable and the B<-w> flag will be ignored for the
+scope of the lexical warning.
+
+=item 5.
+
+The only way to override a lexical warnings setting is with the B<-W>
+or B<-X> command line flags.
+
+=back
+
+The combined effect of 3 & 4 is that it will allow code which uses
+the C<warnings> pragma to control the warning behavior of $^W-type
+code (using a C<local $^W=0>) if it really wants to, but not vice-versa.
+
+=head2 Category Hierarchy
+X<warning, categories>
+
+A hierarchy of "categories" have been defined to allow groups of warnings
+to be enabled/disabled in isolation.
+
+The current hierarchy is:
+
+=for warnings.pl tree-goes-here
+
+Just like the "strict" pragma any of these categories can be combined
-If no import list is supplied, all possible warnings are either enabled
-or disabled.
+ use warnings qw(void redefine);
+ no warnings qw(io syntax untie);
-A number of functions are provided to assist module authors.
+Also like the "strict" pragma, if there is more than one instance of the
+C<warnings> pragma in a given scope the cumulative effect is additive.
+
+ use warnings qw(void); # only "void" warnings enabled
+ ...
+ use warnings qw(io); # only "void" & "io" warnings enabled
+ ...
+ no warnings qw(void); # only "io" warnings enabled
+
+To determine which category a specific warning has been assigned to see
+L<perldiag>.
+
+Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a
+sub-category of the "syntax" category. It is now a top-level category
+in its own right.
+
+=head2 Fatal Warnings
+X<warning, fatal>
+
+The presence of the word "FATAL" in the category list will escalate any
+warnings detected from the categories specified in the lexical scope
+into fatal errors. In the code below, the use of C<time>, C<length>
+and C<join> can all produce a C<"Useless use of xxx in void context">
+warning.
+
+ use warnings;
+
+ time;
+
+ {
+ use warnings FATAL => qw(void);
+ length "abc";
+ }
+
+ join "", 1,2,3;
+
+ print "done\n";
+
+When run it produces this output
+
+ Useless use of time in void context at fatal line 3.
+ Useless use of length in void context at fatal line 7.
+
+The scope where C<length> is used has escalated the C<void> warnings
+category into a fatal error, so the program terminates immediately when it
+encounters the warning.
+
+To explicitly turn off a "FATAL" warning you just disable the warning
+it is associated with. So, for example, to disable the "void" warning
+in the example above, either of these will do the trick:
+
+ no warnings qw(void);
+ no warnings FATAL => qw(void);
+
+If you want to downgrade a warning that has been escalated into a fatal
+error back to a normal warning, you can use the "NONFATAL" keyword. For
+example, the code below will promote all warnings into fatal errors,
+except for those in the "syntax" category.
+
+ use warnings FATAL => 'all', NONFATAL => 'syntax';
+
+As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can
+use:
+
+ use v5.20; # Perl 5.20 or greater is required for the following
+ use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';"
+
+If you want your program to be compatible with versions of Perl before
+5.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In
+previous versions of Perl, the behavior of the statements
+C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and
+C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if
+they included the C<< => 'all' >> portion. As of 5.20, they do.)
+
+B<NOTE:> Users of FATAL warnings, especially
+those using C<< FATAL => 'all' >>
+should be fully aware that they are risking future portability of their
+programs by doing so. Perl makes absolutely no commitments to not
+introduce new warnings, or warnings categories in the future, and indeed
+we explicitly reserve the right to do so. Code that may not warn now may
+warn in a future release of Perl if the Perl5 development team deems it
+in the best interests of the community to do so. Should code using FATAL
+warnings break due to the introduction of a new warning we will NOT
+consider it an incompatible change. Users of FATAL warnings should take
+special caution during upgrades to check to see if their code triggers
+any new warnings and should pay particular attention to the fine print of
+the documentation of the features they use to ensure they do not exploit
+features that are documented as risky, deprecated, or unspecified, or where
+the documentation says "so don't do that", or anything with the same sense
+and spirit. Use of such features in combination with FATAL warnings is
+ENTIRELY AT THE USER'S RISK.
+
+=head2 Reporting Warnings from a Module
+X<warning, reporting> X<warning, registering>
+
+The C<warnings> pragma provides a number of functions that are useful for
+module authors. These are used when you want to report a module-specific
+warning to a calling module has enabled warnings via the C<warnings>
+pragma.
+
+Consider the module C<MyMod::Abc> below.
+
+ package MyMod::Abc;
+
+ use warnings::register;
+
+ sub open {
+ my $path = shift;
+ if ($path !~ m#^/#) {
+ warnings::warn("changing relative path to /var/abc")
+ if warnings::enabled();
+ $path = "/var/abc/$path";
+ }
+ }
+
+ 1;
+
+The call to C<warnings::register> will create a new warnings category
+called "MyMod::Abc", i.e. the new category name matches the current
+package name. The C<open> function in the module will display a warning
+message if it gets given a relative path as a parameter. This warnings
+will only be displayed if the code that uses C<MyMod::Abc> has actually
+enabled them with the C<warnings> pragma like below.
+
+ use MyMod::Abc;
+ use warnings 'MyMod::Abc';
+ ...
+ abc::open("../fred.txt");
+
+It is also possible to test whether the pre-defined warnings categories are
+set in the calling module with the C<warnings::enabled> function. Consider
+this snippet of code:
+
+ package MyMod::Abc;
+
+ sub open {
+ warnings::warnif("deprecated",
+ "open is deprecated, use new instead");
+ new(@_);
+ }
+
+ sub new
+ ...
+ 1;
+
+The function C<open> has been deprecated, so code has been included to
+display a warning message whenever the calling module has (at least) the
+"deprecated" warnings category enabled. Something like this, say.
+
+ use warnings 'deprecated';
+ use MyMod::Abc;
+ ...
+ MyMod::Abc::open($filename);
+
+Either the C<warnings::warn> or C<warnings::warnif> function should be
+used to actually display the warnings message. This is because they can
+make use of the feature that allows warnings to be escalated into fatal
+errors. So in this case
+
+ use MyMod::Abc;
+ use warnings FATAL => 'MyMod::Abc';
+ ...
+ MyMod::Abc::open('../fred.txt');
+
+the C<warnings::warnif> function will detect this and die after
+displaying the warning message.
+
+The three warnings functions, C<warnings::warn>, C<warnings::warnif>
+and C<warnings::enabled> can optionally take an object reference in place
+of a category name. In this case the functions will use the class name
+of the object as the warnings category.
+
+Consider this example:
+
+ package Original;
+
+ no warnings;
+ use warnings::register;
+
+ sub new
+ {
+ my $class = shift;
+ bless [], $class;
+ }
+
+ sub check
+ {
+ my $self = shift;
+ my $value = shift;
+
+ if ($value % 2 && warnings::enabled($self))
+ { warnings::warn($self, "Odd numbers are unsafe") }
+ }
+
+ sub doit
+ {
+ my $self = shift;
+ my $value = shift;
+ $self->check($value);
+ # ...
+ }
+
+ 1;
+
+ package Derived;
+
+ use warnings::register;
+ use Original;
+ our @ISA = qw( Original );
+ sub new
+ {
+ my $class = shift;
+ bless [], $class;
+ }
+
+
+ 1;
+
+The code below makes use of both modules, but it only enables warnings from
+C<Derived>.
+
+ use Original;
+ use Derived;
+ use warnings 'Derived';
+ my $a = Original->new();
+ $a->doit(1);
+ my $b = Derived->new();
+ $a->doit(1);
+
+When this code is run only the C<Derived> object, C<$b>, will generate
+a warning.
+
+ Odd numbers are unsafe at main.pl line 7
+
+Notice also that the warning is reported at the line where the object is first
+used.
+
+When registering new categories of warning, you can supply more names to
+warnings::register like this:
+
+ package MyModule;
+ use warnings::register qw(format precision);
+
+ ...
+
+ warnings::warnif('MyModule::format', '...');
+
+=head1 FUNCTIONS
=over 4
@@ -631,11 +1075,11 @@ Equivalent to:
=item warnings::register_categories(@names)
This registers warning categories for the given names and is primarily for
-use by the warnings::register pragma, for which see L<perllexwarn>.
+use by the warnings::register pragma.
=back
-See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
+See also L<perlmodlib/Pragmatic Modules> and L<perldiag>.
=cut
@@ -685,7 +1129,7 @@ sub bits
return _bits(undef, @_) ;
}
-sub import
+sub import
{
shift;
@@ -698,12 +1142,12 @@ sub import
# append 'all' when implied (after a lone "FATAL" or "NONFATAL")
push @_, 'all' if @_==1 && ( $_[0] eq 'FATAL' || $_[0] eq 'NONFATAL' );
-
+
# Empty @_ is equivalent to @_ = 'all' ;
${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
}
-sub unimport
+sub unimport
{
shift;
@@ -720,7 +1164,7 @@ sub unimport
foreach my $word ( @_ ) {
if ($word eq 'FATAL') {
- next;
+ next;
}
elsif ($catmask = $Bits{$word}) {
$mask &= ~($catmask | $DeadBits{$word} | $All);
diff --git a/t/porting/podcheck.t b/t/porting/podcheck.t
index ac0f2d55c5..cd9057d7c6 100644
--- a/t/porting/podcheck.t
+++ b/t/porting/podcheck.t
@@ -385,6 +385,7 @@ my %excluded_files = (
canonicalize("Porting/perl5200delta.pod") => 1,
canonicalize("Porting/perldelta_template.pod") => 1,
canonicalize("regen/feature.pl") => 1,
+ canonicalize("regen/warnings.pl") => 1,
canonicalize("autodoc.pl") => 1,
canonicalize("configpm") => 1,
canonicalize("miniperl") => 1,
diff --git a/t/porting/regen.t b/t/porting/regen.t
index 1e481a257c..f57b8da690 100644
--- a/t/porting/regen.t
+++ b/t/porting/regen.t
@@ -20,7 +20,7 @@ if ( $Config{usecrosscompile} ) {
skip_all( "Not all files are available during cross-compilation" );
}
-my $tests = 26; # I can't see a clean way to calculate this automatically.
+my $tests = 25; # I can't see a clean way to calculate this automatically.
my %skip = ("regen_perly.pl" => [qw(perly.act perly.h perly.tab)],
"regen/keywords.pl" => [qw(keywords.c keywords.h)],