1

I have written a Perl API and it is used by many programs across teams. I want to track all programs calling my API method. I want to have something like the below

 debug("The calling method is ",  $XXXX); 

How to get $XXXX ?

brian d foy
  • 129,424
  • 31
  • 207
  • 592

2 Answers2

11

perldoc -f caller.

print "The calling function is", (caller 1)[3], "\n";
hobbs
  • 223,387
  • 19
  • 210
  • 288
2

Also see the functions in the Carp module, which wrap the caller function and can serve as a sort of warn function with caller information.

use Carp qw(carp cluck);

carp "This function was called from ";  # caller info will be appended to output

cluck "The full stack trace up to this point is ";
mob
  • 117,087
  • 18
  • 149
  • 283