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 Glossary, 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.
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.
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 Glossary.
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:
It's one of Perl's program-wide special variables (see Chapter 28).
It's fully qualified with its package name (see Chapter 10),
It's imported into the current package (see Chapter 11).
It's masquerading as a lexically scoped variable via an
our
declaration. (This is the main reason we addedour
declarations to Perl.)
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 Glossary for more
information.
Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.