start page | rating of books | rating of authors | reviews | copyrights

Book Home Programming PerlSearch this book

4.9. Pragmas

Many programming languages allow you to give hints to the compiler. In Perl, these hints are conveyed to the compiler with the use declaration. Some pragmas are:

use warnings;
use strict;
use integer;
use bytes;
use constant pi => ( 4 * atan2(1,1) );
Perl pragmas are all described in Chapter 31, "Pragmatic Modules", but right now we'll just talk specifically about a couple that are most useful with the material covered in this chapter.

Although a few pragmas are global declarations that affect global variables or the current package, most are lexically scoped declarations whose effects are constrained to last only until the end of the enclosing block, file, or eval (whichever comes first). A lexically scoped pragma can be countermanded in an inner scope with a no declaration, which works just like use but in reverse.

4.9.1. 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;       # Enable warnings from here till end of file.
...
{
    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 Chapter 33, "Diagnostic Messages".

The use 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 use warnings in Chapter 31, "Pragmatic Modules".

4.9.2. Controlling the Use of Globals

Another commonly seen declaration is the use strict pragma, which has several functions, one of which is to control the use of global variables. Normally, Perl lets you create new globals (or all too often, step on old globals) just by mentioning them. No variable declarations are necessary--by default, that is. Because unbridled use of globals can make large programs or modules painful to maintain, you may sometimes wish to discourage their accidental use. As an aid to preventing such accidents, you can say:

use strict 'vars';
This means that any variable mentioned from here to the end of the enclosing scope must refer either to a lexical variable or to an explicitly allowed global. If it's not one of those, a compilation error results. A global is explicitly allowed if one of the following is true:

Of course, there's always the fifth alternative--if the pragma proves burdensome, simply countermand it within an inner block using:

no strict 'vars'
You can also turn on strict checking of symbolic dereferences and accidental use of barewords with this pragma. Normally people just say:
use strict;
to enable all three strictures. See the use strict entry in Chapter 31, "Pragmatic Modules" for more information.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.