Perl退出警告

yic*_*yic 3 scripting perl warnings

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

怎么能这样做?先感谢您.

too*_*lic 5

警告编译具有致命的选项:

use warnings FATAL => 'all';
Run Code Online (Sandbox Code Playgroud)


mel*_*ene 5

toolic的答案use warnings FATAL => 'all';是正确的,但有一些警告。内部perl函数发出了一些警告,这些警告确实不会消失。在中列出了这些不安全的致命警告perldoc strictures

从版本2.000003开始strictures,它启用以下警告:

use warnings FATAL => 'all';
use warnings NONFATAL => qw(
  exec
  recursion
  internal
  malloc
  newline
  experimental
  deprecated
  portable
);
no warnings 'once';
Run Code Online (Sandbox Code Playgroud)

有关完整的原理,请参见https://metacpan.org/pod/strictures#CATEGORY-SELECTIONS

当然,除了将以上几行复制/粘贴到您的代码中之外,您还可以

use strictures 2;
Run Code Online (Sandbox Code Playgroud)

这也strict为您启用。(不过,您可能必须先安装strictures。)