Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Parent Subroutine

by Bintuch (Acolyte)
on Apr 27, 2010 at 10:21 UTC ( [id://837073]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://837073]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-26 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found