strict “refs”
This generates a runtime error if you try to dereference a string instead of a reference, whether 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, runtime 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 refer only
to package 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 mini symbol table. It’s more
efficient, more readable, and less error prone.
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 ...