<LI><A HREF="#reporting warnings from a module">Reporting Warnings from a Module</A></LI>
</UL>
<LI><A HREF="#todo">TODO</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>perllexwarn - Perl Lexical Warnings</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>The <CODE>use warnings</CODE> pragma is a replacement for both the command line
flag <STRONG>-w</STRONG> and the equivalent Perl variable, <CODE>$^W</CODE>.</P>
<P>The pragma works just like the existing ``strict'' pragma.
This means that the scope of the warning pragma is limited to the
enclosing block. It also means that that the pragma setting will not
leak across files (via <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A>, <A HREF="../../lib/Pod/perlfunc.html#item_require"><CODE>require</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_do"><CODE>do</CODE></A>). This allows
authors to independently define the degree of warning checks that will
be applied to their module.</P>
<P>By default, optional warnings are disabled, so any legacy code that
doesn't attempt to control the warnings will work unchanged.</P>
<P>All warnings are enabled in a block by either of these:</P>
<PRE>
use warnings ;
use warnings 'all' ;</PRE>
<P>Similarly all warnings are disabled in a block by either of these:</P>
<PRE>
no warnings ;
no warnings 'all' ;</PRE>
<P>For example, consider the code below:</P>
<PRE>
use warnings ;
my $a ;
my $b ;
{
no warnings ;
$b = 2 if $a EQ 3 ;
}
$b = 1 if $a NE 3 ;</PRE>
<P>The code in the enclosing block has warnings enabled, but the inner
block has them disabled. In this case that means that the use of the <CODE>EQ</CODE>
operator won't trip a <CODE>"Use of EQ is deprecated"</CODE> warning, but the use of
<CODE>NE</CODE> will produce a <CODE>"Use of NE is deprecated"</CODE> warning.</P>
<P>
<H2><A NAME="default warnings and optional warnings">Default Warnings and Optional Warnings</A></H2>
<P>Before the introduction of lexical warnings, Perl had two classes of
warnings: mandatory and optional.</P>
<P>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 <CODE>"isn't numeric"</CODE>
warning about the ``2:''.</P>
<PRE>
my $a = "2:" + 3;</PRE>
<P>With the introduction of lexical warnings, mandatory warnings now become
<EM>default</EM> 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 <CODE>"isn't numeric"</CODE> warning will only
be reported for the <CODE>$a</CODE> variable.</P>
<PRE>
my $a = "2:" + 3;
no warnings ;
my $b = "2:" + 3;</PRE>
<P>Note that neither the <STRONG>-w</STRONG> flag or the <CODE>$^W</CODE> can be used to
disable/enable default warnings. They are still mandatory in this case.</P>
<P>
<H2><A NAME="what's wrong with w and $^w">What's wrong with <STRONG>-w</STRONG> and <CODE>$^W</CODE></A></H2>
<P>Although very useful, the big problem with using <STRONG>-w</STRONG> 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 <STRONG>-w</STRONG> flag in this case, you
end up enabling warnings in pieces of code that you haven't written.</P>
<P>Similarly, using <CODE>$^W</CODE> 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:</P>
<PRE>
{
local ($^W) = 0 ;
my $a =+ 2 ;
my $b ; chop $b ;
}</PRE>
<P>When this code is run with the <STRONG>-w</STRONG> flag, a warning will be produced
for the <CODE>$a</CODE> line -- <CODE>"Reversed += operator"</CODE>.</P>
<P>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:</P>
<PRE>
{
BEGIN { $^W = 0 }
my $a =+ 2 ;
my $b ; chop $b ;
}</PRE>
<P>The other big problem with <CODE>$^W</CODE> is that way you can inadvertently
change the warning setting in unexpected places in your code. For example,
when the code below is run (without the <STRONG>-w</STRONG> flag), the second call
to <CODE>doit</CODE> will trip a <CODE>"Use of uninitialized value"</CODE> warning, whereas
the first will not.</P>
<PRE>
sub doit
{
my $b ; chop $b ;
}</PRE>
<PRE>
doit() ;</PRE>
<PRE>
{
local ($^W) = 1 ;
doit()
}</PRE>
<P>This is a side-effect of <CODE>$^W</CODE> being dynamically scoped.</P>
<P>Lexical warnings get around these limitations by allowing finer control
over where warnings can or can't be tripped.</P>
<P>
<H2><A NAME="controlling warnings from the command line">Controlling Warnings from the Command Line</A></H2>
<P>There are three Command Line flags that can be used to control when
If the <STRONG>-W</STRONG> flag is used on the command line, it will enable all warnings
throughout the program regardless of whether warnings were disabled
locally using <CODE>no warnings</CODE> or <CODE>$^W =0</CODE>. This includes all files that get
included via <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A>, <A HREF="../../lib/Pod/perlfunc.html#item_require"><CODE>require</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_do"><CODE>do</CODE></A>.
Think of it as the Perl equivalent of the ``lint'' command.