package Bar;
use Foo;

sub bar { fooit "hello from bar"; }

package Foo;

sub fooit {
   # Somehow I want this function to know it was called
   # from the "Bar" module (in this case).
}

最好是在不显式传递包含调用模块名称的参数的情况下完成此操作。

最佳答案

内置的 caller 函数可用于获取有关当前调用堆栈的信息。

 sub fooit {
     my ($pkg, $file, $line) = caller;
     print STDERR "fooit was called from the $pkg package, $file:$line\n";
 }

10-08 07:41