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

Comment on

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

I ran into this before (in perl 5.10) when messing around with attributes. The cause of your problems is that perl attributes are evaluated at compile time, before the subs are evaluated.

Let me back up a bit. Attribute::Handlers is a wrapper around the simpler attribute handling logic in perl. This simpler attribute handling is described in the attributes module. When an attribute is used, perl calls the MODIFY_type_ATTRIBUTE method on the package who owns the sub or variable. In your case type is "CODE". The arguments passed to this method are the package name, coderef, and the attribute text (without the colon). The coderef is a placeholder that is later filled in after perl finishes compiling the sub. Until then, it is empty... ish.

The only way I could think to do get the names would be to save the coderefs for examining later, when all the information is available. Then you can dissect them to get the information you want.

use warnings; use strict; package FooAttr; use Devel::Peek qw(Dump); use B qw(); our @Subs; sub MODIFY_CODE_ATTRIBUTES { my ($pkg, $cref, @attrs) = @_; # Sucks in perl 5.10. Dump($cref); my @bad; for my $attr (@attrs){ if ($attr eq 'FOO') { push @Subs, $cref; } else { push @bad, $attr; } } return @bad; } # Add "FooAttr" to @UNIVERSAL::ISA at *compile-time*. # Otherwise Bar won't find MODIFY_CODE_ATTRIBUTES. BEGIN { push @UNIVERSAL::ISA, __PACKAGE__; } package Bar; use Devel::Peek qw(Dump); # This calls FooAttr::MODIFY_CODE_ATTRIBUTES at *compile-time*. sub baz :FOO {} # Keep in mind this is at runtime. Let's examine the same coderefs! for my $cr (@FooAttr::Subs) { Dump($cr); # This is what Sub::Identify[/Information] does but it # can also use C code to avoid the B module. my $gv = B::svref_2object($cr)->GV; printf "%s::%s\n", $gv->STASH->NAME, $gv->NAME; }

There are zeroes at the top of the output where Devel::Peek's Dump is called by MODIFY_CODE_ATTRIBUTE and the filled in values at the bottom of the output are printed by the runtime code at the end of the Bar module. Keep in mind these are for the same code reference.

Attribute::Handlers (which also adds to @UNIVERSAL::ISA) tries to convert the provided coderef to a symbol by examining all the symbols in the $pkg that is passed to MODIFY_CODE_ATTRIBUTES to see if they equal the coderef. This fails because the symbols are not yet populated in the package's symbol table. Again, this is because weird things are going on at compile-time, that I don't fully understand.

In reply to Re: How to get function's name inside of CODE attribute by juster
in thread How to get function's name inside of CODE attribute by menth0l

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (9)
    As of 2018-06-25 16:25 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (127 votes). Check out past polls.