Skip to Content
Programming Perl, 4th Edition
book

Programming Perl, 4th Edition

by Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
February 2012
Intermediate to advanced
1184 pages
37h 17m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 4th Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 3rd Edition

Programming Perl, 3rd Edition

Larry Wall, Tom Christiansen, Jon Orwant
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes
Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9781449321451Supplemental ContentErrata Page