warnings
use warnings; # same as importing "all"
no warnings; # same as unimporting "all"
use warnings::register;
if (warnings::enabled()) {
warnings::warn("some warning");
}
if (warnings::enabled("void")) {
warnings::warn("void", "some warning");
}
warnings::warnif("Warnings are on");
warnings::warnif("number", "Something is wrong with a number");This lexically scoped pragma permits flexible control over Perl’s built-in warnings, both those emitted by the compiler as well as those from the runtime system.
Once upon a time, the only control you had in Perl over the
treatment of warnings in your program was through either the –w command-line option or the $^W variable. Although useful, these tend to be
all-or-nothing affairs. The –w option
ends up enabling warnings in pieces of module code that you may not have
written, which is occasionally problematic for you and embarrassing for
the original author. Using $^W to
either disable or enable blocks of code can be less than optimal because
it works only during execution time, not during compile time.[263] Another issue is that this program-wide global variable is
scoped dynamically, not lexically. That means that if you enable it in a
block and then from there call other code, you again risk enabling
warnings in code not developed with such exacting standards in
mind.
The warnings pragma circumvents these limitations by being a lexically scoped, compile-time mechanism that permits finer control over where warnings can or can’t be triggered. ...
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