http://qs321.pair.com?node_id=744042


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

I am using this code (which was inspired by Devel::Symdump) to get from coderef to name in my attribute handlers:
# code for this inspired by Devel::Symdump sub _find_name_of_sub_in_pkg{ my ($ref, $pkg) = @_; no strict 'refs'; #return *{$ref}{NAME} if ref $ref eq 'GLOB'; while (my ($key,$val) = each(%{*{"$pkg\::"}})) { local(*ENTRY) = $val; if (defined $val && defined *ENTRY{CODE}) { next unless *ENTRY{CODE} eq $ref; # rewind "each" my $a = scalar keys %{*{"$pkg\::"}}; return $key; } } die "failed to find name for StartRunmode code ref $ref in package + $pkg\n"; }
This only works if you know which package you are looking at, but I think in your case, you do.

Update: Another major catch is that depending on which phase the handler runs, the subroutine will not yet be installed in the symbol table, so there is no name yet. Attribute::Handlers I think allows you to run at CHECK, when it may work. Maybe. Attributes are not for the faint of heart.

Update 2: I probably managed to confuse not only myself by now, but have a look at this:

use strict; package Beth; use Attribute::Handlers; sub print_name { my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_; if (ref $symbol eq 'GLOB'){ print *{$symbol}{NAME}; } print "\n"; } sub Lion :ATTR(CODE,BEGIN) { print_name(@_); } sub Tiger :ATTR(CODE,CHECK) { print_name(@_); } sub bar : Lion { print "pling.\n"; } sub foo : Tiger { print "pling.\n"; }
The CHECK phase attribute can print the name by just looking at the GLOB it gets, whereas it is still anonymous for the BEGIN attribute. Not sure how to write anything except BEGIN handlers without Attribute::Handlers (only with plain MODIFY_CODE_ATTRIBUTES).

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 12:31 UTC
    Attributes are not for the faint of heart

    Ain't that the truth! Thank you so much for the suggestion, but it didn't work within MODIFY_CODE_ATTRIBUTES. Sigh... Your "another major catch" seems to be right on. Your proposal exhibits the same behavior as Devel::Peek::Dump - works great after compilation (even in a BEGIN block) but not in MODIFY_CODE_ATTRIBUTES. The association between symbol and name does not appear to be set up until after the MODIFY_CODE_ATTRIBUTES function completes. This small script...

    outputs...

    Dumping CODE(0x81a8e60) in MODIFY_CODE_ATTRIBUTES: Warning: could not find name for CODE(0x81a8e60) at Monks/Snippet.pm l +ine 23. Name via Thilosophy method: <undef> Dumping CODE(0x81a8e60) in BEGIN block after compilation. Name via Thilosophy method: <bar>

    I am more than a little surprised that it is so difficult to use MODIFY_CODE_ATTRIBUTES to redefine functions. When I scanned the web last night in response to your query on attribute handling it seemed that one of the major use case for attributes is decorating functions. It seems a little strange to add a feature that can't be used for one of its major use cases.

    Best, beth

    Update: fixed some typos, added comment about blocks, some clarifications.

    Further update: Just reread the Perl documentation for attributes and I think I finally understand what the last sentence of this quote might mean. Do you suppose "cloned copies ... used as closures" means decorated functions?... and the reason for this non-implementation being that the coderef is only partially set up? (i.e. not part yet of the symbol table)

    WARNING: the mechanisms described here are still experimental. Do not rely on the current implementation. In particular, there is no provision for applying package attributes to 'cloned' copies of subroutines used as closures.

    It does appear that the only way to get decorated functions is to set up CHECK handlers or do ugly things like store a hash keyed by code ref and then match up the names and the coderefs later on, a la Class::Declare.

      I hate to bring this up, but while we are at it: the CHECK (and INIT) phases do not really work under mod_perl ...

      I am more than a little surprised that it is so difficult to use MODIFY_CODE_ATTRIBUTES to redefine functions.

      I think this part of Perl is a construction site that has been abandoned half-finished. Which only makes it more interesting to the enterprising hacker...

      I am now looking at ways to get a similarly concise syntax as attributes offer using other (more established) mechanisms. Any ideas?