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


in reply to Using caller to determine the namespace of an anonymous subroutine

As an alternate to what chromatic said, I have found the Sub::Name module to be very helpful in these kinds of situations (I used it in my module Class::MOP, whenever I am adding a method to a class). Again, it needs to be within the code for Creator::create, but IMO it is much less "magical".

my $full_name = $caller."::".$field; *{$fullname} = Sub::Name::subname $full_name => sub { ... }
After this, not only will the sub show up correctly in the output of caller(), but is also shows up correctly when you poke at it with any of the B modules too. This is because Sub::Name will actually set the STASH and NAME attributes for the CV itself using XS.

-stvn

Replies are listed 'Best First'.
Re^2: Using caller to determine the namespace of an anonymous subroutine
by rrwo (Friar) on Feb 26, 2006 at 12:08 UTC

    Sub::Name might be a better solution, since I don't need access to the innards of the coderef. Though I'm not sure I'd call it less magical, since I still need to assign the subroutine name to the coderef.

    Thanks again!