Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Is there a way to access the name under which a subroutine was first defined?

by Anonymous Monk
on Feb 16, 2009 at 07:32 UTC ( [id://743993]=note: print w/replies, xml ) Need Help??


in reply to Is there a way to access the name under which a subroutine was first defined?

possible to determine the name of a sub from a sub reference?
  • Comment on Re: Is there a way to access the name under which a subroutine was first defined?

Replies are listed 'Best First'.
Re^2: Is there a way to access the name under which a subroutine was first defined?
by ELISHEVA (Prior) on Feb 16, 2009 at 08:42 UTC
    Thanks! It didn't occur to me to search for "sub reference" as a synonym for "coderef" or "code reference"!

    It will take me some time to go through the various suggestions and links, but from a brief scan, most of the suggestions, bar one, seem to rely on one or more B:: modules. I am looking for a solution that could be used on non-development machines, so those don't really apply.

    There is one, however, that looks very interesting: Devel::Peek. What I'm not clear on though is how that relates to ordinary Perl data types. The documentation seems to be written with XS developers in mind (of which I am not one). I would be grateful for a simple demo of how that module might be used within a MODIFY_CODE_ATTRIBUTES function to decorate a Perl (not XS) subroutine, e.g. with logging code before and after the original subroutine code.

    MODIFY_CODE_ATTRIBUTES only gets the code reference to which the attribute is attached, not the name. Without the name, there is no way to use the attribute to "decorate" foo(...) with before and after code, and then assign the decorated subroutine back to foo(...), like this:

    package XYZ; sub MODIFY_CODE_ATTRIBUTES { my $sPackage = shift @_; my $crSub = shift @_; foreach my $sAttribute (@_) { if ($sAttribute eq 'MagicLogger') { my $sName = mythical_original_name_finder($crSub); *{$sName} = sub { #fancy logging stuff before &$crSub(@_); #fancy logging stuff after } } } }

    Yes, yes, I know this can be done without attributes. There are loads of CPAN function decorator modules, but I'd like to know if it can be done using the current attribute interface. The most commonly recommended method for doing this, Attribute::Handlers, does not play nicely with the FETCH and MODIFY functions of the currently supported interface (nor should it - it was written in 2001). The issue is discussed in some detail here.

    My feeling is that the decoration implementation would be a lot cleaner if we had an attribute handler module that used FETCH and MODIFY. But to do that, I need a way to get from the coderef back to the original name of the function so I can reassign a new subroutine to the symbol table.

    And yes, yes, I know that the attribute interface is still experimental and even if we can find something now it may not work in the next release. Still, I'd like to know.

    Best, beth

    Update:

    Just took a look at Devel::Peek. I don't know that this will help - it appears that the package's MODIFY_CODE_ATTRIBUTES function is called before the association between the original name and coderef is set up. Here's the output when Devel::Peek::Dump is called from within a MODIFY_CODE_ATTRIBUTES method:

    The GVGV::GV value is supposed to be the name of the subroutine (see here), but in this case it is set to 0x0.

      seem to rely on one or more B:: modules.

      What issue do you have with B? B is an interface to the guts, something you will have to access in this case. Whether B is used or not, the same code will get executed one way or another to get the info you want.

        No issue with B:: - just had never looked at the core list carefully enough to notice that the many of them were part of it. I'm forever learning new things about Perl.

        But would B:: help where Devel::Peek did not (in the MODIFY_CODE_ATTRIBUTES sub)? What core B:: modules would you suggest I study?

        Best, beth

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://743993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found