perl – 函数可以告诉它从哪个模块调用吗?
发布时间:2020-12-15 23:22:17 所属栏目:大数据 来源:网络整理
导读: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).} 优选地,这将在没有显式传递包含调用模块名称的参数的情况下完成. 解决
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:$linen"; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |