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

Kind Monks, How can I get the name of the subroutine which a certain code was called from? Thanks.

Replies are listed 'Best First'.
Re: Parent Subroutine
by szabgab (Priest) on Apr 27, 2010 at 10:31 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Parent Subroutine
by arc_of_descent (Hermit) on Apr 27, 2010 at 10:32 UTC
Re: Parent Subroutine
by Marshall (Canon) on Apr 27, 2010 at 14:20 UTC
    sub whoami { (caller(1))[3] }; #returns name of the sub that called # this sub sub whowasi { (caller(2))[3] }; #returns name of the parent of the # sub that called this sub do_calc (1,2,3); sub do_calc { print "In subroutine: do_calc()\n"; print "I am: ",whoami,"\n\n"; do_sub_calc(); } sub do_sub_calc { print "In subroutine do_sub_calc()\n"; print "I am: ", whoami(),"\n"; print "I was called by: ", whowasi(),"\n"; } __END__ prints: In subroutine: do_calc() I am: main::do_calc In subroutine do_sub_calc() I am: main::do_sub_calc I was called by: main::do_calc
Re: Parent Subroutine
by Herkum (Parson) on Apr 27, 2010 at 16:30 UTC

    Better to use Carp for tracing a call stack than caller.

      If you only want one layer back, use carp(). And if you want the whole call stack, use cluck(). But if you want anything in between, you need to build it yourself with caller().

      - doug

Re: Parent Subroutine
by doug (Pilgrim) on Apr 27, 2010 at 18:44 UTC

    perldoc -f caller