如何知道函数是从哪个perl模块或文件调用的

myzjeezk  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(186)

我需要知道我的函数是从哪个Perl文件调用的。假设我有一个模块(MyModule.pm),它有以下函数:

package MyModule;

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

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

use MyModule;
MyModule->myFunc();

我需要在此处调用myFunc()以打印“myScript.pl
在Perl中有什么方法可以做到这一点吗?

lztngnrs

lztngnrs1#

在方法内部,

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

有关详细信息,请查看perldoc -f caller

相关问题