Perl退出警告

时间:2017-03-25 17:34:20

标签: perl scripting warnings

我想要一个perl脚本在任何类型的警告上立即退出并显示错误代码。 例如,在“参数...不是数字”警告。

怎么能这样做? 提前谢谢。

3 个答案:

答案 0 :(得分:5)

warnings编译指示具有FATAL选项:

use warnings FATAL => 'all';

答案 1 :(得分:4)

工具的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了解完整的理由。

不是将上述行复制/粘贴到您的代码中,您当然可以

use strictures 2;

也为您启用strict。 (但您可能必须先安装strictures。)

答案 2 :(得分:1)

尚未提及,但您可以设置__WARN__处理程序并执行您喜欢的操作。

$SIG{__WARN__} = sub {
    die "This program does not tolerate warnings like: @_";        
};