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.

[14] Sure, you could read the documentation, but your system administrator compressed the documentation and broke it.

use Devel::Symdump; my %existing; BEGIN {     my $symbols = Devel::Symdump->new( 'main' );     @existing{ $symbols->functions( ) } = ( ); } use File::Spec::Functions; BEGIN {     my $symbols   = Devel::Symdump->new( 'main' );     my @new_funcs =         map { s/main:://; $_ }         grep { not exists $existing{ $_ } } $symbols->functions( );     local $" = "\\n  ";     warn qq|Imported:$"@new_funcs\\n|; }

As of Perl 5.8.7, this prints:

$ perl show_fsf_symbols.pl Imported:   catfile   curdir   updir   path   file_name_is_absolute   no_upwards   canonpath   catdir   rootdir $

Are you worried that this won't account for user-defined functions? Don'tby the time the BEGIN blocks run, Perl hasn't seen any yet. You're safe.


Hacking the Hack

Devel::Symdump works on more than just functions. You can find all exported scalars, arrays, hashes, and file and directory handles, as well as all other symbol tables beneath the named one. Beware, though, that all new symbols have a scalar created by default (at least in Perl prior to 5.9.3), so searching for those isn't as useful as you might think.

It would be easy to register a list of all exported functions with the using package, to allow more introspection and runtime. You could even write a module that does this and re-exports them to your package.



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net