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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to get function's name inside of CODE attribute (monkey see)
by tye (Sage) on Oct 05, 2011 at 18:40 UTC | |
by stvn (Monsignor) on Oct 05, 2011 at 19:29 UTC | |
by tye (Sage) on Oct 05, 2011 at 19:44 UTC | |
by chromatic (Archbishop) on Oct 05, 2011 at 20:06 UTC | |
Re^2: How to get function's name inside of CODE attribute
by menth0l (Monk) on Oct 06, 2011 at 08:14 UTC |