Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Which module contains an imported subroutine

by john.deighan (Novice)
on Mar 26, 2013 at 17:20 UTC ( #1025563=perlquestion: print w/ replies, xml ) Need Help??
john.deighan has asked for the wisdom of the Perl Monks concerning the following question:

If I have a script that "uses" multiple Perl modules, and I know that a function named f was imported from one of them, i.e. it was not defined in the script itself, is there a way to determine the name of the module that f was imported from?

Comment on Which module contains an imported subroutine
Re: Which module contains an imported subroutine
by toolic (Chancellor) on Mar 26, 2013 at 17:48 UTC
    B::Xref
    $ cat script.pl use Carp; carp(777); $ perl -MO=Xref script.pl File ..../linux/x86_64/5u5/lib/perl5/5.12.2/Carp.pm Subroutine Carp::carp
Re: Which module contains an imported subroutine
by clueless newbie (Friar) on Mar 26, 2013 at 18:02 UTC
Re: Which module contains an imported subroutine
by runrig (Abbot) on Mar 26, 2013 at 18:12 UTC
    If I have a script that "uses" multiple Perl modules, and I know that a function named f was imported from one of them, i.e. it was not defined in the script itself

    Don't do that. Be explicit about what you import.

    use Coneheads qw( GnarfleTheGarthock );

      This can’t be emphasized enough.   A couple of years ago I was wrestling with a really old application which “inexplicably” did not run on one of several different machines.   It turned out to be a problem such as this:   there was a common subroutine-name, more than one potential source of it, and a very slight difference in the file setup on that one machine ... a file was there but not being picked up, long story.   But the bottom line was that the intended (correct ...) behavior of that code as-written was in effect contextually determined, with an ambiguity in play that was hiding the root problem.   Learning from this, I now try to be as explicit as possible in every place where such ambiguities might occur: using qw() import-lists with large “kitchen sink” utility packages, and sometimes empty lists there with explicit fully-qualified (i.e. with package name expressly specified) references to the exact routine that I need.   These problems, when I have seen them, come from software that was originally written for older Perls.

Re: Which module contains an imported subroutine
by john.deighan (Novice) on Mar 26, 2013 at 20:32 UTC

    Thanks to ysth and his response to the question "How to de-reference a coderef? (B tricks)", I came up with the following function:

    sub SourcePackage { my($funcname, $package) = @_; $package = caller() if !$package; return if ! $package->can($funcname); my $coderef; { no strict 'refs'; $coderef = \&{"$package\:\:$funcname"}; } return B::svref_2object($coderef)->GV->STASH->NAME; } # SourcePackage()

    So, in the situation I described, where a script imports from multiple Perl modules, e.g. "use X; use Y; use Z;", and you want to know which module a function $funcname was imported from, you call find out with:

    my $srcPkg = SourcePackage($funcname, __PACKAGE__);

    which will return the name of the package that $funcname was imported from (and into package __PACKAGE__ - i.e. the current package), or undef if no function by that name is in the symbol table of package __PACKAGE__ (which in the case of a script without a package declaration, will be 'main').

    FYI, the { and } around "no strict 'refs';" was to restrict the "no strict" to the smallest code possible. The function SourcePackage() can be put into an external library of utilities, which is why I created it so that the package name must be passed in - however, by default, if you don't pass in a package name, it will use the package that the SourcePackage() subroutine was called from, which I'm sure would be the most common use case. So, the above call can be abbreviated to:

    my $srcPkg = SourcePackage($funcname);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025563]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-09-20 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls