How can I print the calling program/module inside a method in Perl?

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 ?

Upvotes: 1

Views: 4334

Answers (2)

mob
mob

Reputation: 118595

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 ";

Upvotes: 2

hobbs
hobbs

Reputation: 239652

perldoc -f caller.

print "The calling function is", (caller 1)[3], "\n";

Upvotes: 11

Related Questions