Skip to Content
Perl Hacks
book

Perl Hacks

by Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe
May 2006
Beginner
298 pages
6h 51m
English
O'Reilly Media, Inc.
Content preview from Perl Hacks

Hack #75. Find All Symbols in a Package

Explore symbol tables without soft references.

One of the earliest temptations for novice programmers is to use the contents of one variable as part of the name of another variable. After making one too many costly mistakes or showing such code to a more experienced programmer, novices start to use the strict pragma to warn them about dubious constructs.

However, several advanced features of Perl, such as the implementation of the Exporter module, are only possible by reading from and writing to the symbol table at run time. Normally strict forbids this—but it's possible to access global symbols at run time with strict enabled.

This is an easy way to find out if a symbol—such as a scalar, array, hash, subroutine, or filehandle—exists.

The Hack

Suppose you want to check whether a specific type of variable is present in a given namespace. You need to know the name of the package, the name of the variable, and the type of the variable.

Defining the following subroutine in the UNIVERSAL package makes the class method contains_symbol available to any package:[4]

my %types = ( '$' => 'SCALAR', '@' => 'ARRAY', '%' => 'HASH', '*' => 'IO', '&' => 'CODE', ); sub UNIVERSAL::contains_symbol { my ($namespace, $symbol) = @_; my @keys = split( /::/, $namespace ); my $type = $types{ substr( $symbol, 0, 1, '' ) } || 'SCALAR'; my $table = \\%main::; for my $key (@keys) { $key .= '::'; return 0 unless exists $table->{$key}; $table = $table->{$key}; } return 0 unless ...
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

Perl Debugged

Perl Debugged

Peter Scott, Ed Wright
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Learning Perl 6

Learning Perl 6

brian d foy
Perl by Example

Perl by Example

Ellie Quigley

Publisher Resources

ISBN: 0596526741Supplemental ContentErrata Page