use strict
use 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.
strict 'refs'
This generates a run-time error if you use symbolic references, intentionally or otherwise. See Chapter 8 for more about these.
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.
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 ...
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.