您如何最好地将Perl方法中的错误归因于调用者

问题描述

|| 给定一种可能因警告和/或错误而失败的方法,我希望error方法显示在调用方处。杉木实例此脚本:
foo(0);         # line 1

sub foo {
    1 / shift;  # line 4
}
产生错误
Illegal division by zero at foo.pl line 4
,但我想要
Illegal division by zero at foo.pl line 1
。如果将方法放在模块中或将方法主体包装在eval中,应该有几种方法,但是我没有找到这样的简单方法:
sub foo {
    attributeErrorsToCaller; # do some magic
    1 / shift;
}
有这种方法吗? 编辑:mirod \的答案接近不是我在寻找什么:
Foo::foo(0);         # line 1

package Foo;
use diagnostics -traceonly;
BEGIN { disable diagnostics; }

sub foo {
    enable diagnostics;
    1 / shift;       # line 9
}
如果没有
enable diagnostics
,则错误消息为
Illegal division by zero at foo.pl line 9.
。对于
enable diagnostics
,它仍然太冗长,但这也可能有用:
Uncaught exception from user code:
    Illegal division by zero at foo.pl line 10.
 at foo.pl line 10
     Foo::foo(0) called at foo.pl line 2
我敢打赌,我可以破解诊断程序以确切地获得我想要的功能,但是可能更建议使用诊断程序作为原始模块。     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)