Skip to Main Content
Perl Hacks
book

Perl Hacks

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

Hack #79. Find Imported Functions

Keep an eye on your namespace.

Importing functions is a mixed blessing. Having functions available from another namespace without having to type their full names is convenient. However, the chance for name collisions and confusion increases with the number of imported symbols.

There are multiple ways to tell the original package of a function, but many of them involve lots of deep magic and, in cases of generated functions, may not tell the whole story. If you really want to know what you've imported and when, the shortest and simplest approach is to use the Devel::Symdump module.

The Hack

To get a list of functions from a package, create a new Devel::Symdump object and use the functions( ) method on it:

use Devel::Symdump;
my $symbols   = Devel::Symdump->new( 'main' );
my @functions = $symbols->functions( );

That gives you a list of fully-qualified function names as of the time of the call. Load and import from the other modules you need, and then create and query a new Devel::Symdump object to get a longer list of functions.

Running the Hack

Suppose you want to know what File::Spec::Functions imports.[14] If you can wedge the code to create and query the first Devel::Symdump object before the use line executes [Hack #70], all you have to do is perform an array intersection to remove duplicate elements.

use Devel::Symdump; my %existing; BEGIN { my $symbols = Devel::Symdump->new( 'main' ); @existing{ $symbols->functions( ) } = ( ); } use File::Spec::Functions; ...
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 Testing: A Developer's Notebook

Perl Testing: A Developer's Notebook

Ian Langworth, Chromatic
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Advanced Perl Programming

Advanced Perl Programming

Sriram Srinivasan
Perl Debugged

Perl Debugged

Peter Scott, Ed Wright

Publisher Resources

ISBN: 0596526741Supplemental ContentErrata Page