我有一个模块需要在BEGIN块中进行一些检查.这可以防止用户在线下看到无用的消息(在编译阶段,在第二个BEGIN中看到).
问题是,如果我在BEGIN内部死亡,我抛出的信息就会被埋没
BEGIN failed--compilation aborted at.但是我更喜欢die,exit 1因为它可以被捕获.我应该使用exit 1或者有什么我可以做的来抑制这个额外的消息吗?
#!/usr/bin/env perl
use strict;
use warnings;
BEGIN {
my $message = "Useful message, helping the user prevent Horrible Death";
if ($ENV{AUTOMATED_TESTING}) {
# prevent CPANtesters from filling my mailbox
print $message;
exit 0;
} else {
## appends: BEGIN failed--compilation aborted at
## which obscures the useful message
die $message;
## this mechanism means that the error is not trappable
#print $message;
#exit 1;
}
}
BEGIN {
die "Horrible Death with useless message.";
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*rom 11
当您die抛出一个在较早的调用级别捕获的异常时.die从BEGIN块中捕获的唯一处理程序是编译器,它会自动附加您不想要的错误字符串.
为避免这种情况,您可以使用exit 1找到的解决方案,也可以安装新的模具处理程序:
# place this at the top of the BEGIN block before you try to die
local $SIG{__DIE__} = sub {warn @_; exit 1};
Run Code Online (Sandbox Code Playgroud)