【问题标题】:How to know from which perl module or file the function was called如何知道函数是从哪个 perl 模块或文件调用的
【发布时间】:2015-04-12 17:40:47
【问题描述】:

我需要知道我的函数是从哪个 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 中做到这一点?

【问题讨论】:

    标签: perl


    【解决方案1】:

    内部方法,

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2016-01-20
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      相关资源
      最近更新 更多