Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which subroutine was I in?

Status
Not open for further replies.

kempis555

Programmer
Jan 2, 2001
77
Is there any variable or function that I can use to know which subroutine is currently being executed?

This would be a big help in speeding up programming.
For instance,

sub one{
open(INFILE, "infile.txt") || &bug("Couldn't open file in subroutine $which_subroutine");
...
}

sub two{
open(INFILE, "infile.txt") || &bug("Couldn't open file in subroutine $which_subroutine");
...
}

if the file can't be opened in sub one, it's $which_subroutine has a value of "one"... likewise "two" for two.

Or maybe there's a better way to do all this?
Thanks in advance

-kempis555
 
Try using the caller function. To find the CURRENT values pass it a value of 0. To find out the values of the CALLING subroutine, pass it a value of 1. E.G:
Code:
local($pkg, $file, $line, $subrtn, $has, $want) = caller(1);
You may need to experiment with the value passed to caller() to get the ones you're looking for, but it works. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top