Controlling Warnings
To show how this works, we’ll manipulate the warnings pragma to
tell Perl whether to issue warnings for questionable
practices:
use warnings; # or explicitly enable warnings
...
{
no warnings; # Disable warnings through end of block.
...
}
# Warnings are automatically enabled again here.Once warnings are enabled, Perl complains about variables used only once, variable declarations that mask other declarations in the same scope, improper conversions of strings into numbers, using undefined values as legitimate strings or numbers, trying to write to files you only opened read-only (or didn’t open at all), and many other conditions documented in perldiag.
The warnings pragma is the
preferred way to control warnings. Old programs could only use the
–w command-line switch or modify
the global $^W variable:
{
local $^W = 0;
...
}It’s much better to use the use
warnings and no
warnings pragmas. A pragma is better because it happens at
compile time, because it’s a lexical declaration and therefore
cannot affect code it wasn’t intended to affect, and because
(although we haven’t shown you in these simple examples) it affords
fine-grained control over discrete classes of warnings. For more
about the warnings pragma,
including how to convert merely noisy warnings into fatal errors,
and how to override the pragma to turn on warnings globally even if
a module says not to, see warnings in Chapter 29.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access