我在我的Logger模块中覆盖了我的SIG模具处理程序.
# Catch die messages and log them with logdie
$SIG{__DIE__} = \&logdie;
现在,程序按预期运行,将调用后处理.
use strict;
use warnings;
use File::Path;
# use MyLogger;
my $dir="/random";
eval {
# local $SIG{__DIE__};
File::Path::make_path($dir);
};
if($@) {
warn("Cannot create $dir :$@ \n");
}
print "Post processing \n";
但是,如果我包含我的记录器模块并添加使用MyLogger,则代码在eval语句中失败并出现以下错误,并且不会调用后处理.
[错误] 2015/04/27 22:19:07 Carp.pm:166\u0026gt; mkdir / random:./test.pl第11行的权限被拒绝.
修复此问题的一个选项是添加本地sigdie句柄(如注释代码所示).
但是,我的记录器模块被许多脚本使用.
解决方法
$^S指示当前执行点是否在eval块内:
$^S State --------- ------------------------------------- undef Parsing module,eval,or main program true (1) Executing an eval false (0) Otherwise
所以听起来你想在__DIE__处理程序的开头检查$^ S是否为真:
package MyLogger;
sub logdie {
$^S && die $_[0]; # use default die handler
... # else use custom die handler
}
