ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankovperllexwarn - Perl Lexical Warnings
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe=head1 DESCRIPTION
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweThe C<use warnings> pragma is a replacement for both the command line
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweflag B<-w> and the equivalent Perl variable, C<$^W>.
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri PankovThe pragma works just like the existing "strict" pragma.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweThis means that the scope of the warning pragma is limited to the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweenclosing block. It also means that the pragma setting will not
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweleak across files (via C<use>, C<require> or C<do>). This allows
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweauthors to independently define the degree of warning checks that will
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowebe applied to their module.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweBy default, optional warnings are disabled, so any legacy code that
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowedoesn't attempt to control the warnings will work unchanged.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweAll warnings are enabled in a block by either of these:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe use warnings ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe use warnings 'all' ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweSimilarly all warnings are disabled in a block by either of these:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe no warnings ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe no warnings 'all' ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweFor example, consider the code below:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe use warnings ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe no warnings ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe my $b = @a[0] ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe my $c = @a[0];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweThe code in the enclosing block has warnings enabled, but the inner
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweblock has them disabled. In this case that means the assignment to the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowescalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowewarning, but the assignment to the scalar C<$b> will not.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe=head2 Default Warnings and Optional Warnings
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweBefore the introduction of lexical warnings, Perl had two classes of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowewarnings: mandatory and optional.
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri PankovAs its name suggests, if your code tripped a mandatory warning, you
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankovwould get a warning whether you wanted it or not.
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri PankovFor example, the code below would always produce an C<"isn't numeric">
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankovwarning about the "2:".
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov my $a = "2:" + 3;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweWith the introduction of lexical warnings, mandatory warnings now become
c10c16dec587a0662068f6e2991c29ed3a9db943Richard LoweI<default> warnings. The difference is that although the previously
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowemandatory warnings are still enabled by default, they can then be
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowesubsequently enabled or disabled with the lexical warning pragma. For
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweexample, in the code below, an C<"isn't numeric"> warning will only
disable/enable default warnings. They are still mandatory in this case.
Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
disable/enable default warnings.
to be enabled/disabled in isolation.
warnings::warn("changing relative path to /var/abc")
$path = "/var/abc/$path";
called "MyMod::abc", i.e. the new category name matches the current
abc::open("../fred.txt");
MyMod::Abc::open('../fred.txt');
Odd numbers are unsafe at main.pl line 7