Symbol Table References
In unusual circumstances, you might not know what type of
reference you need when your program is written. A reference can be
created by using a special syntax, affectionately known as the
*foo{THING} syntax. *foo{THING} returns a reference to the
THING slot in *foo, which is the symbol table entry
holding the values of $foo,
@foo, %foo, and friends.
$scalarref = *foo{SCALAR}; # Same as \$foo
$arrayref = *ARGV{ARRAY}; # Same as \@ARGV
$hashref = *ENV{HASH}; # Same as \%ENV
$coderef = *handler{CODE}; # Same as \&handler
$globref = *foo{GLOB}; # Same as \*foo
$ioref = *STDIN{IO}; # Er...
$formatref = *foo{FORMAT}; # More er...All of these are self-explanatory except for the last two.
*foo{FORMAT} is how to get at the
object that was declared using the format statement. There isn’t much you can
do with one of those that’s very interesting.
On the other hand, *STDIN{IO} yields the actual internal
IO::Handle object that the typeglob contains; that is, the part
of the typeglob that the various I/O functions are actually
interested in. For compatibility with old versions of Perl, *foo{FILEHANDLE} was once a synonym for
the hipper *foo{IO} notation, but
that use is now deprecated.
In theory, you can use a *
anywhere you’d use a HANDLE{IO}* or a
HANDLE\*, such as for passing handles into or out of subroutines, or storing them in larger data structures. (In practice, there are still some wrinkles to be ironed out.) The advantage of them is that they access only ...HANDLE
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.
Read now
Unlock full access