in reply to How to de-reference a coderef?
Unlike e.g. array refs, every code ref has a backreference to its glob (so that when you call it, caller can know the sub name). This is fairly easily accessible with the B module (which provides introspection facilities for perl thingies):
Returns something like pkgname::__ANON__ for anonymous subs, or undef if something goes wrong (like being passed an array ref instead of a code ref).use B; sub coderef2name { eval { my $obj = B::svref_2object( shift() ); $obj->GV->STASH->NAME . "::" . $obj->GV->NAME; } || undef; }
Updated to use GV->STASH->NAME, not STASH->NAME. Thanks, betterworld
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to de-reference a coderef? (B tricks)
by Anonymous Monk on Aug 24, 2011 at 01:14 UTC | |
by ikegami (Patriarch) on Aug 25, 2011 at 04:29 UTC | |
by Anonymous Monk on Aug 25, 2011 at 01:14 UTC |
In Section
Seekers of Perl Wisdom