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

标签 perl

我需要知道我的函数是从哪个 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 中有什么方法可以做到这一点吗?

最佳答案

内部方法,

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

检查 perldoc -f caller了解更多详情。

关于perl - 如何知道函数是从哪个 perl 模块或文件调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29587638/

相关文章:

regex - 停止失控的正则表达式

Perl File::Find::Rule 从数组中排除目录

linux - 调试 smtp 连接

string - 如何通过perl中的每个字符比较2个字符串

perl - 带括号和不带括号的eof之间的区别? (Perl5)

python - 逐行读取文件,并为耗时过长的行设置超时?

css - 无法将 css 合并到 perl cgi

perl 使用 shift 命令解析命令行参数

perl - 如何仅在您指定的某个任意字段上进行正则表达式替换

html - 如何使用 Perl 向网页添加图形?