我有一个模块,它需要在BEGIN块中做一些检查。这可以防止用户看到一条无用的消息(在编译阶段,在这里的second 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.";
}发布于 2011-10-26 23:04:15
当你使用die的时候,你抛出了一个在早期调用级别被捕获的异常。从BEGIN块捕获die的惟一处理程序是编译器,它会自动附加您不想要的错误字符串。
要避免这种情况,您可以使用您找到的exit 1解决方案,也可以安装新的模具处理程序:
# place this at the top of the BEGIN block before you try to die
local $SIG{__DIE__} = sub {warn @_; exit 1};https://stackoverflow.com/questions/7904510
复制相似问题