31.19. use strictuse strict; # Install all three strictures. use strict "vars"; # Variables must be predeclared. use strict "refs"; # Can't use symbolic references. use strict "subs"; # Bareword strings must be quoted. use strict; # Install all... no strict "vars"; # ...then renege on one. This lexically scoped pragma changes some basic rules about what Perl considers to be legal code. Sometimes these restrictions seem too strict for casual programming, such as when you're just trying to whip up a five-line filter program. The larger your program, the more you need to be strict about it. Currently, there are three possible things to be strict about: subs, vars, and refs. If no import list is supplied, all three restrictions are assumed. 31.19.1. strict 'refs'This generates a run-time error if you use symbolic references, intentionally or otherwise. See Chapter 8, "References" for more about these. Symbolic references are suspect for various reasons. It's surprisingly easy for even well-meaning programmers to invoke them accidentally; strict 'refs' guards against that. Unlike real references, symbolic references can only refer to global variables. They aren't reference-counted. And there's often a better way to do what you're doing: instead of referencing a symbol in a global symbol table, use a hash as its own little mini-symbol table. It's more efficient, more readable, and less error prone.use strict 'refs'; $ref = \$foo; # Store "real" (hard) reference. print $$ref; # Dereferencing is ok. $ref = "foo"; # Store name of global (package) variable. print $$ref; # WRONG, run-time error under strict refs. Nevertheless, some sorts of valid manipulation really do require direct access to the package's global symbol table of variables and function names. For example, you might want to examine the @EXPORT list or the @ISA superclass of a given package whose name you don't know in advance. Or you might want to install a whole slew of function calls that are all aliases to the same closure. This is just what symbolic references are best at, but to use them while use strict is in effect, you must first undo the "refs" stricture: # make a bunch of attribute accessors for my $methname (qw/name rank serno/) { no strict 'refs'; *$methname = sub { $_[0]->{ __PACKAGE__ . $methname }; } 31.19.2. strict 'vars'Under this stricture, a compile-time error is triggered if you attempt to access a variable that hasn't met at least one of the following criteria:
Just using a local operator isn't good enough to keep use strict 'vars' happy because, despite its name, that operator doesn't change whether the named variable is global or not. It just gives the variable a new, temporary value for the duration of block at run time. You still need to use our to declare a global variable, or my to declare a lexical variable. You can, however, localize an our: Globals predefined by Perl are exempt from these requirements. This applies to program-wide globals (those forced into package main like @ARGV or $_) and to per-package variables like $a and $b, which are normally used by the sort function. Per-package variables used by modules like Exporter still need to be declared using our:local our $law = "martial"; our @EXPORT_OK = qw(name rank serno); 31.19.3. strict 'subs'This stricture makes Perl treat all barewords as syntax errors. A bareword ("bearword" in some dialects) is any bare name or identifier that has no other interpretation forced by context. (Context is often forced by a nearby keyword or token, or by predeclaration of the word in question.) Historically, barewords were interpreted as unquoted strings. This stricture outlaws that interpretation. If you mean to use it as a string, quote it. If you mean to use it as a function call, predeclare it or use parentheses. As a particular case of forced context, remember that a word that appears by itself in curly braces or on the lefthand side of the => operator counts as being quoted, and so is not subject to this restriction. use strict 'subs'; $x = whatever; # WRONG: bareword error! $x = whatever(); # This always works, though. sub whatever; # Predeclare function. $x = whatever; # Now it's ok. # These uses are permitted, because the => quotes: %hash = (red => 1, blue => 2, green => 3); $rednum = $hash{red}; # Ok, braces quote here. # But not this one: @coolnums = @hash{blue, green}; # WRONG: bareword error. @coolnums = @hash{"blue", "green"}; # Ok, words now quoted. @coolnums = @hash{qw/blue green/}; # Likewise. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|