Morad
Morad

Reputation: 2779

How to know from which perl module or file the function was called

I need to know from which perl file my function was called. Let's say I have the following module (MyModule.pm) which has the following function:

package MyModule;

sub myFunc{
   # Here I need to print the name of the file that the function was called from
}

And the script myScript.pl uses MyModule and calls the function myFunc():

use MyModule;
MyModule->myFunc();

I need the call to myFunc() here to print "myScript.pl"

Is there any way to do this in Perl?

Upvotes: 3

Views: 1295

Answers (1)

mpapec
mpapec

Reputation: 50637

Inside method,

sub myFunc{
  my ($package, $filename, $line) = caller;
  print $filename;
}

Check perldoc -f caller for more details.

Upvotes: 6

Related Questions