Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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);

In reply to Re: Which module contains an imported subroutine by john.deighan
in thread Which module contains an imported subroutine by john.deighan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (10)
    As of 2014-09-01 07:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (299 votes), past polls