是否可以在Perl中启用/禁用基于ARGV的使用严格/警告?
我尝试了这段代码,但它不起作用.我认为它应该在’$x = 2’的行中产生警告错误;
# Do this at the beginning of the script BEGIN { if ( $ARGV[0] =~ /^Y$/i ) { use strict; use warnings; } else { no strict; no warnings; } } $x = 2; print "x is $x\n";
目的是仅在开发中启用警告消息.
解决方法
use strict;
相当于
BEGIN { require strict; import strict; }
所以使用严格的效果;是无条件的(因为import strict;在if之前评估).
此外,使用严格和使用警告的效果都是词法范围的,因此它们的效果仅限于它们一如既往的位置.
使用
no strict; # Probably not actually needed. no warnings; # Probably not actually needed. use if scalar( $ARGV[0] =~ /^Y\z/i ),'strict'; use if scalar( $ARGV[0] =~ /^Y\z/i ),'warnings';