Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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).

In reply to Re: Is there a way to access the name under which a subroutine was first defined? by Thilosophy
in thread Is there a way to access the name under which a subroutine was first defined? by ELISHEVA

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-08-03 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (38 votes). Check out past polls.

    Notices?