http://www.perlmonks.org?node_id=929838


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.