Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to get function's name inside of CODE attribute

by chromatic (Archbishop)
on Oct 05, 2011 at 16:49 UTC ( #929838=note: print w/ replies, xml ) Need Help??


in reply to How to get function's name inside of CODE attribute

You can't always get a name (as attributes can apply to anonymous functions), but Sub::Information can help:

use 5.014; use Sub::Information; use Attribute::Lexical 'CODE:FOO' => \&handle_foo_attr; sub handle_foo_attr { my ($func, $identifier, $attrs, $caller) = @_; my $info = Sub::Information->new( $func ); my $name = $info->name; say "Function '$name' has FOO attribute"; }

I added in Attribute::Lexical to avoid the need to monkeypatch UNIVERSAL.


Improve your skills with Modern Perl: the free book.


Comment on Re: How to get function's name inside of CODE attribute
Select or Download Code
Re^2: How to get function's name inside of CODE attribute (monkey see)
by tye (Cardinal) on Oct 05, 2011 at 18:40 UTC
    I added in Attribute::Lexical to avoid the need to monkeypatch UNIVERSAL.

    You don't have to read much of the code before you get to:

    unshift @UNIVERSAL::ISA, "Attribute::Lexical::UNIVERSAL";

    So, this module avoids adding methods to UNIVERSAL (I assume), but adding a super class after the fact is certainly also monkey patching.

    (Yes, I actually implement attributes without monkey patching UNIVERSAL at all and much more simply and, IMHO, sanely and buttoning that up for CPAN is on my to-do list.)

    - tye        

      Yes, I actually implement attributes without monkey patching UNIVERSAL at all and much more simply and, IMHO, sanely and buttoning that up for CPAN is on my to-do list.

      Is the code sane enough for github? or some other public code hosting site? I would like to see this.

      -stvn

        Actually, much of the code is included in Devel::Init. After the __END__ line is even the code I decided to leave out of Devel::Init that tries to provide compatibility with the first entrant, Attribute::Handlers.

        No, Devel::Init is not a tool for enabling developers to easily define their own attributes. But the code in Devel::Init only requires small additions and minor reworking for it to become that.

        - tye        

      I'm no fan of that myself, but it's less troublesome than putting all handlers in UNIVERSAL. I'll happily switch to a better solution that avoids modifying global behavior altogether.


      Improve your skills with Modern Perl: the free book.

Re^2: How to get function's name inside of CODE attribute
by menth0l (Monk) on Oct 06, 2011 at 08:14 UTC
    Using Sub::Information didn't help me. It works fine under 5.12 but has the same effect on 5.10 as in my previous example, i.e. $info->name returns empty string.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-09-20 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls