perl – 抑制“BEGIN失败 – 编译中止”
发布时间:2020-12-15 21:43:55 所属栏目:大数据 来源:网络整理
导读:我有一个模块需要在BEGIN块中进行一些检查.这可以防止用户在线下看到无用的消息(在编译阶段,在第二个BEGIN中看到). 问题是,如果我在BEGIN内部死亡,我抛出的信息就会被埋没 BEGIN失败了 – 编译中止了.但是我更喜欢死掉1,因为它可以被捕获.我应该只使用退出1
我有一个模块需要在BEGIN块中进行一些检查.这可以防止用户在线下看到无用的消息(在编译阶段,在第二个BEGIN中看到).
问题是,如果我在BEGIN内部死亡,我抛出的信息就会被埋没 #!/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."; } 解决方法
当你死亡时,你会抛出一个在较早的通话级别被捕获的异常.从BEGIN块中捕获die的唯一处理程序是编译器,它会自动附加你不想要的错误字符串.
为避免这种情况,您可以使用找到的exit 1解决方案,也可以安装新的die处理程序: # place this at the top of the BEGIN block before you try to die local $SIG{__DIE__} = sub {warn @_; exit 1}; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |