.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "PERLLEXWARN 1"
.TH PERLLEXWARN 1 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
perllexwarn \- Perl Lexical Warnings
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
The \f(CW\*(C`use warnings\*(C'\fR pragma is a replacement for both the command line
flag \fB\-w\fR and the equivalent Perl variable, \f(CW$^W\fR.
.PP
The pragma works just like the existing \*(L"strict\*(R" 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 \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR). This allows
authors to independently define the degree of warning checks that will
be applied to their module.
.PP
By default, optional warnings are disabled, so any legacy code that
doesn't attempt to control the warnings will work unchanged.
.PP
All warnings are enabled in a block by either of these:
.PP
.Vb 2
\& use warnings ;
\& use warnings 'all' ;
.Ve
.PP
Similarly all warnings are disabled in a block by either of these:
.PP
.Vb 2
\& no warnings ;
\& no warnings 'all' ;
.Ve
.PP
For example, consider the code below:
.PP
.Vb 7
\& use warnings ;
\& my @a ;
\& {
\& no warnings ;
\& my $b = @a[0] ;
\& }
\& my $c = @a[0];
.Ve
.PP
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 \f(CW$c\fR will trip the \f(CW"Scalar value @a[0] better written as $a[0]"\fR
warning, but the assignment to the scalar \f(CW$b\fR will not.
.Sh "Default Warnings and Optional Warnings"
.IX Subsection "Default Warnings and Optional Warnings"
Before the introduction of lexical warnings, Perl had two classes of
warnings: mandatory and optional.
.PP
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 \f(CW"isn't numeric"\fR
warning about the \*(L"2:\*(R".
.PP
.Vb 1
\& my $a = "2:" + 3;
.Ve
.PP
With the introduction of lexical warnings, mandatory warnings now become
\&\fIdefault\fR 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 \f(CW"isn't numeric"\fR warning will only
be reported for the \f(CW$a\fR variable.
.PP
.Vb 3
\& my $a = "2:" + 3;
\& no warnings ;
\& my $b = "2:" + 3;
.Ve
.PP
Note that neither the \fB\-w\fR flag or the \f(CW$^W\fR can be used to
disable/enable default warnings. They are still mandatory in this case.
.ie n .Sh "What's wrong with \fB\-w\fP and $^W"
.el .Sh "What's wrong with \fB\-w\fP and \f(CW$^W\fP"
.IX Subsection "What's wrong with -w and $^W"
Although very useful, the big problem with using \fB\-w\fR 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 \fB\-w\fR flag in this case, you
end up enabling warnings in pieces of code that you haven't written.
.PP
Similarly, using \f(CW$^W\fR 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:
.PP
.Vb 5
\& {
\& local ($^W) = 0 ;
\& my $a =+ 2 ;
\& my $b ; chop $b ;
\& }
.Ve
.PP
When this code is run with the \fB\-w\fR flag, a warning will be produced
for the \f(CW$a\fR line \*(-- \f(CW"Reversed += operator"\fR.
.PP
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:
.PP
.Vb 5
\& {
\& BEGIN { $^W = 0 }
\& my $a =+ 2 ;
\& my $b ; chop $b ;
\& }
.Ve
.PP
The other big problem with \f(CW$^W\fR 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 \fB\-w\fR flag), the second call
to \f(CW\*(C`doit\*(C'\fR will trip a \f(CW"Use of uninitialized value"\fR warning, whereas
the first will not.
.PP
.Vb 4
\& sub doit
\& {
\& my $b ; chop $b ;
\& }
.Ve
.PP
.Vb 1
\& doit() ;
.Ve
.PP
.Vb 4
\& {
\& local ($^W) = 1 ;
\& doit()
\& }
.Ve
.PP
This is a side-effect of \f(CW$^W\fR being dynamically scoped.
.PP
Lexical warnings get around these limitations by allowing finer control
over where warnings can or can't be tripped.
.Sh "Controlling Warnings from the Command Line"
.IX Subsection "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:
.IP "\fB\-w\fR" 5
.IX Item "-w"
This is the existing flag. If the lexical warnings pragma is \fBnot\fR
used in any of you code, or any of the modules that you use, this flag
will enable warnings everywhere. See \*(L"Backward Compatibility\*(R" for
details of how this flag interacts with lexical warnings.
.IP "\fB\-W\fR" 5
.IX Item "-W"
If the \fB\-W\fR flag is used on the command line, it will enable all warnings
throughout the program regardless of whether warnings were disabled
locally using \f(CW\*(C`no warnings\*(C'\fR or \f(CW\*(C`$^W =0\*(C'\fR. This includes all files that get
included via \f(CW\*(C`use\*(C'\fR, \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`do\*(C'\fR.
Think of it as the Perl equivalent of the \*(L"lint\*(R" command.
.IP "\fB\-X\fR" 5
.IX Item "-X"
Does the exact opposite to the \fB\-W\fR flag, i.e. it disables all warnings.
.Sh "Backward Compatibility"
.IX Subsection "Backward Compatibility"
If you are used with working with a version of Perl prior to the
introduction of lexically scoped warnings, or have code that uses both
lexical warnings and \f(CW$^W\fR, this section will describe how they interact.
.PP
How Lexical Warnings interact with \fB\-w\fR/\f(CW$^W\fR:
.IP "1." 5
If none of the three command line flags (\fB\-w\fR, \fB\-W\fR or \fB\-X\fR) that
control warnings is used and neither \f(CW$^W\fR or the \f(CW\*(C`warnings\*(C'\fR 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.
.IP "2." 5
The \fB\-w\fR flag just sets the global \f(CW$^W\fR variable as in 5.005 \*(-- this
means that any legacy code that currently relies on manipulating \f(CW$^W\fR
to control warning behavior will still work as is.
.IP "3." 5
Apart from now being a boolean, the \f(CW$^W\fR variable operates in exactly
the same horrible uncontrolled global way, except that it cannot
disable/enable default warnings.
.IP "4." 5
If a piece of code is under the control of the \f(CW\*(C`warnings\*(C'\fR pragma,
both the \f(CW$^W\fR variable and the \fB\-w\fR flag will be ignored for the
scope of the lexical warning.
.IP "5." 5
The only way to override a lexical warnings setting is with the \fB\-W\fR
or \fB\-X\fR command line flags.
.PP
The combined effect of 3 & 4 is that it will allow code which uses
the \f(CW\*(C`warnings\*(C'\fR pragma to control the warning behavior of $^W\-type
code (using a \f(CW\*(C`local $^W=0\*(C'\fR) if it really wants to, but not vice\-versa.
.Sh "Category Hierarchy"
.IX Subsection "Category Hierarchy"
A hierarchy of \*(L"categories\*(R" have been defined to allow groups of warnings
to be enabled/disabled in isolation.
.PP
The current hierarchy is:
.PP
.Vb 93
\& all -+
\& |
\& +- closure
\& |
\& +- deprecated
\& |
\& +- exiting
\& |
\& +- glob
\& |
\& +- io -----------+
\& | |
\& | +- closed
\& | |
\& | +- exec
\& | |
\& | +- layer
\& | |
\& | +- newline
\& | |
\& | +- pipe
\& | |
\& | +- unopened
\& |
\& +- misc
\& |
\& +- numeric
\& |
\& +- once
\& |
\& +- overflow
\& |
\& +- pack
\& |
\& +- portable
\& |
\& +- recursion
\& |
\& +- redefine
\& |
\& +- regexp
\& |
\& +- severe -------+
\& | |
\& | +- debugging
\& | |
\& | +- inplace
\& | |
\& | +- internal
\& | |
\& | +- malloc
\& |
\& +- signal
\& |
\& +- substr
\& |
\& +- syntax -------+
\& | |
\& | +- ambiguous
\& | |
\& | +- bareword
\& | |
\& | +- digit
\& | |
\& | +- parenthesis
\& | |
\& | +- precedence
\& | |
\& | +- printf
\& | |
\& | +- prototype
\& | |
\& | +- qw
\& | |
\& | +- reserved
\& | |
\& | +- semicolon
\& |
\& +- taint
\& |
\& +- threads
\& |
\& +- uninitialized
\& |
\& +- unpack
\& |
\& +- untie
\& |
\& +- utf8
\& |
\& +- void
\& |
\& +- y2k
.Ve
.PP
Just like the \*(L"strict\*(R" pragma any of these categories can be combined
.PP
.Vb 2
\& use warnings qw(void redefine) ;
\& no warnings qw(io syntax untie) ;
.Ve
.PP
Also like the \*(L"strict\*(R" pragma, if there is more than one instance of the
\&\f(CW\*(C`warnings\*(C'\fR pragma in a given scope the cumulative effect is additive.
.PP
.Vb 5
\& 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
.Ve
.PP
To determine which category a specific warning has been assigned to see
perldiag.
.PP
Note: In Perl 5.6.1, the lexical warnings category \*(L"deprecated\*(R" was a
sub-category of the \*(L"syntax\*(R" category. It is now a top-level category
in its own right.
.Sh "Fatal Warnings"
.IX Subsection "Fatal Warnings"
The presence of the word \*(L"\s-1FATAL\s0\*(R" 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 \f(CW\*(C`time\*(C'\fR, \f(CW\*(C`length\*(C'\fR
and \f(CW\*(C`join\*(C'\fR can all produce a \f(CW"Useless use of xxx in void context"\fR
warning.
.PP
.Vb 1
\& use warnings ;
.Ve
.PP
.Vb 1
\& time ;
.Ve
.PP
.Vb 4
\& {
\& use warnings FATAL => qw(void) ;
\& length "abc" ;
\& }
.Ve
.PP
.Vb 1
\& join "", 1,2,3 ;
.Ve
.PP
.Vb 1
\& print "done\en" ;
.Ve
.PP
When run it produces this output
.PP
.Vb 2
\& Useless use of time in void context at fatal line 3.
\& Useless use of length in void context at fatal line 7.
.Ve
.PP
The scope where \f(CW\*(C`length\*(C'\fR is used has escalated the \f(CW\*(C`void\*(C'\fR warnings
category into a fatal error, so the program terminates immediately it
encounters the warning.
.PP
To explicitly turn off a \*(L"\s-1FATAL\s0\*(R" warning you just disable the warning
it is associated with. So, for example, to disable the \*(L"void\*(R" warning
in the example above, either of these will do the trick:
.PP
.Vb 2
\& no warnings qw(void);
\& no warnings FATAL => qw(void);
.Ve
.PP
If you want to downgrade a warning that has been escalated into a fatal
error back to a normal warning, you can use the \*(L"\s-1NONFATAL\s0\*(R" keyword. For
example, the code below will promote all warnings into fatal errors,
except for those in the \*(L"syntax\*(R" category.
.PP
.Vb 1
\& use warnings FATAL => 'all', NONFATAL => 'syntax';
.Ve
.Sh "Reporting Warnings from a Module"
.IX Subsection "Reporting Warnings from a Module"
The \f(CW\*(C`warnings\*(C'\fR 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 \f(CW\*(C`warnings\*(C'\fR
pragma.
.PP
Consider the module \f(CW\*(C`MyMod::Abc\*(C'\fR below.
.PP
.Vb 1
\& package MyMod::Abc;
.Ve
.PP
.Vb 1
\& use warnings::register;
.Ve
.PP
.Vb 8
\& sub open {
\& my $path = shift ;
\& if ($path !~ m#^/#) {
\& warnings::warn("changing relative path to /tmp/")
\& if warnings::enabled();
\& $path = "/tmp/$path" ;
\& }
\& }
.Ve
.PP
.Vb 1
\& 1 ;
.Ve
.PP
The call to \f(CW\*(C`warnings::register\*(C'\fR will create a new warnings category
called \*(L"MyMod::abc\*(R", i.e. the new category name matches the current
package name. The \f(CW\*(C`open\*(C'\fR 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 \f(CW\*(C`MyMod::Abc\*(C'\fR has actually
enabled them with the \f(CW\*(C`warnings\*(C'\fR pragma like below.
.PP
.Vb 4
\& use MyMod::Abc;
\& use warnings 'MyMod::Abc';
\& ...
\& abc::open("../fred.txt");
.Ve
.PP
It is also possible to test whether the pre-defined warnings categories are
set in the calling module with the \f(CW\*(C`warnings::enabled\*(C'\fR function. Consider
this snippet of code:
.PP
.Vb 1
\& package MyMod::Abc;
.Ve
.PP
.Vb 5
\& sub open {
\& warnings::warnif("deprecated",
\& "open is deprecated, use new instead") ;
\& new(@_) ;
\& }
.Ve
.PP
.Vb 3
\& sub new
\& ...
\& 1 ;
.Ve
.PP
The function \f(CW\*(C`open\*(C'\fR has been deprecated, so code has been included to
display a warning message whenever the calling module has (at least) the
\&\*(L"deprecated\*(R" warnings category enabled. Something like this, say.
.PP
.Vb 4
\& use warnings 'deprecated';
\& use MyMod::Abc;
\& ...
\& MyMod::Abc::open($filename) ;
.Ve
.PP
Either the \f(CW\*(C`warnings::warn\*(C'\fR or \f(CW\*(C`warnings::warnif\*(C'\fR 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
.PP
.Vb 4
\& use MyMod::Abc;
\& use warnings FATAL => 'MyMod::Abc';
\& ...
\& MyMod::Abc::open('../fred.txt');
.Ve
.PP
the \f(CW\*(C`warnings::warnif\*(C'\fR function will detect this and die after
displaying the warning message.
.PP
The three warnings functions, \f(CW\*(C`warnings::warn\*(C'\fR, \f(CW\*(C`warnings::warnif\*(C'\fR
and \f(CW\*(C`warnings::enabled\*(C'\fR 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.
.PP
Consider this example:
.PP
.Vb 1
\& package Original ;
.Ve
.PP
.Vb 2
\& no warnings ;
\& use warnings::register ;
.Ve
.PP
.Vb 5
\& sub new
\& {
\& my $class = shift ;
\& bless [], $class ;
\& }
.Ve
.PP
.Vb 4
\& sub check
\& {
\& my $self = shift ;
\& my $value = shift ;
.Ve
.PP
.Vb 3
\& if ($value % 2 && warnings::enabled($self))
\& { warnings::warn($self, "Odd numbers are unsafe") }
\& }
.Ve
.PP
.Vb 7
\& sub doit
\& {
\& my $self = shift ;
\& my $value = shift ;
\& $self->check($value) ;
\& # ...
\& }
.Ve
.PP
.Vb 1
\& 1 ;
.Ve
.PP
.Vb 1
\& package Derived ;
.Ve
.PP
.Vb 8
\& use warnings::register ;
\& use Original ;
\& our @ISA = qw( Original ) ;
\& sub new
\& {
\& my $class = shift ;
\& bless [], $class ;
\& }
.Ve
.PP
.Vb 1
\& 1 ;
.Ve
.PP
The code below makes use of both modules, but it only enables warnings from
\&\f(CW\*(C`Derived\*(C'\fR.
.PP
.Vb 7
\& use Original ;
\& use Derived ;
\& use warnings 'Derived';
\& my $a = new Original ;
\& $a->doit(1) ;
\& my $b = new Derived ;
\& $a->doit(1) ;
.Ve
.PP
When this code is run only the \f(CW\*(C`Derived\*(C'\fR object, \f(CW$b\fR, will generate
a warning.
.PP
.Vb 1
\& Odd numbers are unsafe at main.pl line 7
.Ve
.PP
Notice also that the warning is reported at the line where the object is first
used.
.SH "TODO"
.IX Header "TODO"
.Vb 4
\& perl5db.pl
\& The debugger saves and restores C<$^W> at runtime. I haven't checked
\& whether the debugger will still work with the lexical warnings
\& patch applied.
.Ve
.PP
.Vb 5
\& diagnostics.pm
\& I *think* I've got diagnostics to work with the lexical warnings
\& patch, but there were design decisions made in diagnostics to work
\& around the limitations of C<$^W>. Now that those limitations are gone,
\& the module should be revisited.
.Ve
.PP
.Vb 1
\& document calling the warnings::* functions from XS
.Ve
.SH "SEE ALSO"
.IX Header "SEE ALSO"
warnings, perldiag.
.SH "AUTHOR"
.IX Header "AUTHOR"
Paul Marquess
|