perl – 函数可以告诉它从哪个模块调用吗?

前端之家收集整理的这篇文章主要介绍了perl – 函数可以告诉它从哪个模块调用吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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";
 }

猜你在找的Perl相关文章