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

Re: Module caller

by choroba (Cardinal)
on May 06, 2015 at 13:36 UTC ( [id://1125836]=note: print w/replies, xml ) Need Help??


in reply to Module caller

Yes, caller should do what you want.

MyModule.pm:

package MyModule; use warnings; use strict; print __PACKAGE__, ' called from ', (caller)[1], ".\n"; sub func { return 2 * shift } __PACKAGE__

script.pl:

#!/usr/bin/perl use warnings; use strict; use MyModule; warn MyModule::func(21);

Output:

MyModule called from ./script.pl. 42 at ./script.pl line 8.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Module caller
by marinersk (Priest) on May 06, 2015 at 17:50 UTC

    Make sure you read the documetation on how callerworks -- knowing how many layers back to go, and what is actually supported on your platform (notably if Windows), might take some playing with it to see what you get.

Re^2: Module caller
by jeffsto (Novice) on May 07, 2015 at 13:02 UTC
    print __PACKAGE__, ' called from ', (caller)1, ".\n"; Worked perfect! Its exactly what I was looking for. Thanks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found