禁止“开始失败——编译中止于”
我有一个模块需要在 BEGIN 块中进行一些检查。这可以防止用户看到无用的消息(在编译阶段,在第二个 BEGIN 中看到)。
问题是,如果我死在 BEGIN 内,我抛出的消息就会被埋在后面 BEGIN 失败 - 编译中止于
。不过,我更喜欢 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.";
}
I have a module which needs to do some checking in a BEGIN block. This prevents the user from seeing a useless message down the line (during compile phase, seen in second BEGIN here).
The problem is that if I die inside the BEGIN the message I throw gets buried behindBEGIN failed--compilation aborted at
. However I prefer die
to exit 1
since it would then be trappable. Should I just use exit 1
or is there something I can do to suppress this additional message?
#!/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.";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您
死亡
时,您将抛出一个在较早的调用级别捕获的异常。唯一能够从BEGIN
块捕获die
的处理程序是编译器,它会自动附加您不需要的错误字符串。为了避免这种情况,您可以使用您找到的
exit 1
解决方案,也可以安装新的芯片处理程序:When you
die
you are throwing an exception that gets caught at an earlier call level. The only handler that will catch thedie
from yourBEGIN
block is the compiler, and that is automatically attaching the error string you don't want.To avoid this, you can either use the
exit 1
solution you found, or you can install a new die handler: