|
|
| P is for Practical | |
| PerlMonks |
Re: How to get function's name inside of CODE attributeby juster (Friar) |
| on Oct 06, 2011 at 14:59 UTC ( #929999=note: 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.
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 Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||