Документация
HTML CSS PHP PERL другое

5.4 use warnings in Detail

 
Previous Table of Contents Next

5.4 use warnings in Detail

use warnings is equivalent to saying:


use warnings 'all';

which is in turn short for:


use warnings 'chmod';

use warnings 'closure';

use warnings 'exiting';

# ...Lots more of the same...

that is, it enables a whole host of warnings. It is a fairly recent pragma (introduced in Perl 5.6) and you might not have seen it; you are more likely to be familiar with its venerable command-line cousin, -w:


#!/usr/bin/perl -w

# Now warnings are enabled

So why so much extra typing for something that does the same job? Because use warnings is much more powerful. You can disable parts of it instead of the all-or-nothing proposition presented by -w (more on that in the next section), and you can also restrict its scope lexically. You can also use it to turn off some warnings that are otherwise enabled even if you don't use -w (the so-called default warnings). As if that isn't enough, you can selectively turn warnings into fatal exceptions, and you can even make up your own warning classes.

5.4.1 use warnings vs. Modules

Because of the lexical scoping, use warnings will not propagate its effect to modules you use (whereas -w does). In your own modules, add use warnings near the top. Other people's modules may or may not be so enlightened. Now, it's really the module author's business and responsibility to make their code warnings-safe, but sometimes the author of a module you are using may not have put a use warnings statement in it.

Why should you care? Because quite often you can get a clue that there's something wrong with your code from a warning generated inside a module. You may have created an undefined value somewhere and passed it into a module where it finally triggered a "use of uninitialized value" warning. That's helpful. And you'd get that warning with -w. But with use warnings, you won't get it unless the module author also had a use warnings statement.

Therefore, for maximum safety when using third-party modules, add the -w flag to your shebang line. As regressive as that may sound, it will ensure that you see every warning that you can. Wherever a warnings pragma appears, whether in your code or a module you use, the -w flag for the code under the control of that pragma will be ignored by Perl, so you still benefit from the additional pragma power where it applies.

You can make all warnings fatal with the FATAL argument:

use warnings FATAL => 'all';

That turns all warnings (or whatever warning classes you specify) into fatal exceptions. Now your code will simply die when it would generate a warning.

I'll talk about how best to deal with warnings when writing your own modules in Section 10.4.

5.4.2 Pragmas, Meet AutoLoader

If you use the AutoLoader module to load subroutines on demand, be aware that any strict or warnings pragmas in the main module will not be visible to the autoloaded subroutines. You need to add use strict and use warnings to the beginning of each autoloaded subroutine for them to take effect.

    Previous Table of Contents Next
    © 2000- NIV