如何知道调用函数的perl模块或文件

Mor*_*rad 3 perl

我需要知道我的函数调用了哪个perl文件.假设我有以下模块(MyModule.pm),它具有以下功能:

package MyModule;

sub myFunc{
   # Here I need to print the name of the file that the function was called from
}
Run Code Online (Sandbox Code Playgroud)

并且脚本myScript.pl使用MyModule并调用函数myFunc():

use MyModule;
MyModule->myFunc();
Run Code Online (Sandbox Code Playgroud)

我需要打电话到myFunc()这里打印" myScript.pl"

在Perl中有没有办法做到这一点?

Сух*_*й27 6

内部方法,

sub myFunc{
  my ($package, $filename, $line) = caller;
  print $filename;
}
Run Code Online (Sandbox Code Playgroud)

查看perldoc -f caller更多详细信息.