Mass Deface
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 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 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
used in any of you code, or any of the modules that you use, this flag
will enable warnings everywhere. See L 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 or C<$^W =0>. This includes all files that get
included via C