警告时退出 Perl
Perl exit on warning
我希望 perl 脚本在出现任何类型的警告时立即退出并显示错误代码。
例如,在 "argument ... isn't numeric in addition" 警告时。
如何做到这一点?
提前谢谢你。
warnings pragma 有致命选项:
use warnings FATAL => 'all';
toolic 对 use warnings FATAL => 'all';
的回答是正确的,但有一些注意事项。内部 perl 函数发出了一些警告,它们真的不希望死亡。 perldoc strictures
.
中列出了那些不安全的致命警告
从 strictures
的版本 2.000003 开始,它启用如下警告:
use warnings FATAL => 'all';
use warnings NONFATAL => qw(
exec
recursion
internal
malloc
newline
experimental
deprecated
portable
);
no warnings 'once';
请参阅 https://metacpan.org/pod/strictures#CATEGORY-SELECTIONS 了解完整的基本原理。
您当然可以 copy/pasting 而不是将上述代码行 copy/pasting
use strictures 2;
这也为您启用了 strict
。
(不过,您可能必须先安装 strictures
。)
尚未提及,但您可以设置一个 __WARN__
处理程序并在那里执行您喜欢的操作。
$SIG{__WARN__} = sub {
die "This program does not tolerate warnings like: @_";
};
我希望 perl 脚本在出现任何类型的警告时立即退出并显示错误代码。 例如,在 "argument ... isn't numeric in addition" 警告时。
如何做到这一点? 提前谢谢你。
warnings pragma 有致命选项:
use warnings FATAL => 'all';
toolic 对 use warnings FATAL => 'all';
的回答是正确的,但有一些注意事项。内部 perl 函数发出了一些警告,它们真的不希望死亡。 perldoc strictures
.
从 strictures
的版本 2.000003 开始,它启用如下警告:
use warnings FATAL => 'all';
use warnings NONFATAL => qw(
exec
recursion
internal
malloc
newline
experimental
deprecated
portable
);
no warnings 'once';
请参阅 https://metacpan.org/pod/strictures#CATEGORY-SELECTIONS 了解完整的基本原理。
您当然可以 copy/pasting 而不是将上述代码行 copy/pasting
use strictures 2;
这也为您启用了 strict
。
(不过,您可能必须先安装 strictures
。)
尚未提及,但您可以设置一个 __WARN__
处理程序并在那里执行您喜欢的操作。
$SIG{__WARN__} = sub {
die "This program does not tolerate warnings like: @_";
};