I'm sure it must be feasable to set the value for the constant based on a command line switch. I'm not sure if one can just plug in use a module like Getopt::Std or Getopt::Long for this — they probably work at runtime, and might interfere with your own use of command line switches. Hence the handcrafted code, in the example:
{
my $debug;
BEGIN {
if(grep { $_ eq '-d' } @ARGV) { # look for switch
$debug = 1;
@ARGV = grep { $_ ne '-d' } @ARGV; # remove switch
}
}
use constant DEBUG => $debug;
}
print "Debug is on" if DEBUG;
Does anybody else get a "Useless use of a constant in void context" warning for the print line if DEBUG is off, or is it my old Perl version (5.6.1)? -MO=Deparse tells me it got replaced by the statement '???';, which might explain it. It's still silly. |