in reply to How to get the name of an aliased function

In addition to all the great ways described above, you can also use Sub::Uplevel with an extra function call. (Technically, Sub::Uplevel replaces caller with code that knows how to skip frames in the stack on request.)

use Sub::Uplevel; sub MainSub { my $name = (caller 0)[3]; print "My name is $name\n"; } sub sub1 { uplevel 1, \&MainSub }; sub sub2 { uplevel 1, \&MainSub }; sub sub3 { uplevel 1, \&MainSub }; sub1(); sub2(); sub3();

Prints:

My name is main::sub1 My name is main::sub2 My name is main::sub3

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.