Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Finding what called a subroutine

by davido (Archbishop)
on Apr 05, 2013 at 23:46 UTC ( #1027231=note: print w/replies, xml ) Need Help??


in reply to Finding what called a subroutine

See Carp:

use Carp qw(croak); sub foo { croak; } foo();

Here, carp reports from the perspective of the caller. In this case, it will report both the line in the sub that died, and the line in the calling code that called the sub that died.

If you run the code above as "mytest.pl", unmodified it would output the following:

at ./mytest.pl line 4. main::foo() called at ./mytest.pl line 7

The Carp module also provides "confess" which will offer a more detailed back-trace.

If you just want to log calls you can use Carp's "carp" and "cluck" functions to get similar output to warn, but from the caller's perspective.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027231]
help
Chatterbox?
[Discipulus]: ovedpo15 A subroutine may be called using an explicit & prefix. The & is optional in modern Perl... see perlsub in docs and search for ampersand
[Discipulus]: it has some, even dangerous, implication
[Discipulus]: I still use but I also attract many critics for this: I use when I call subs defined in the very same file, just to recognize them. You can avoid (but sometimes is needed)
[marto]: believe it or not this is a SPAM account :P
[Discipulus]: it seems a legitimate one.. grin ..

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2018-05-22 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?