Is it possible to enable/disable use strict / warnings based on ARGV in perl?
I tried this code but it doesn't work. I believe it should generate a warning error at line where '$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";
The purpose is to enable warning messages only in development.
use strict;
is equivalent to
BEGIN {
require strict;
import strict;
}
so the effect of use strict; are unconditional (since import strict; is evaluated before the if is evaluated).
Furthermore, the effects of both use strict and use warnings are lexically-scoped, so their effects are limited to the curlies in which they are located as always.
Use
BEGIN {
if ( $ARGV[0] =~ =~ /^Y\z/i ) {
require strict;
import strict;
require warnings;
import warnings;
}
}
or
use if scalar( $ARGV[0] =~ /^Y\z/i ), 'strict';
use if scalar( $ARGV[0] =~ /^Y\z/i ), 'warnings';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With