princepawn has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a number of subroutines which populate a hashref with their activity based on how they are:
sub load_data { my $self = shift; $self->{$0} = 'data i created'; }
but of course $0 doesnt change roles within subroutines.

Replies are listed 'Best First'.
Re: How can a subroutine get its name?
by clemburg (Curate) on Sep 03, 2001 at 20:56 UTC

    caller() is the function you want. A subroutine can get its own name like this:

    perl -e 'sub try { print +(caller(0))[3] }; try() '

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: How can a subroutine get its name?
by tachyon (Chancellor) on Sep 03, 2001 at 20:58 UTC

    The caller() function will do the trick:

    gosub(); sub gosub { print "my name is " . get_name(); print "\nI can get it here to, my name " . (caller(0))[3]; } sub get_name { (caller(1))[3]; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print