I am currently trying to set a debug flag using command-line arguments in Perl and I seem to be having problems with something which I thought was pretty easy.
my $debugvalue;
my $file = $ARGV[0] or die;
if ($ARGV[1] == "debug")
{
$debugvalue = 1;
}else
{
$debugvalue = 0;
}
I am looking to enter a file followed by a word purely saying debug, if it doesn’t then set the flag to 0.
test.pl file.txt debug
-
Would set the flag to 1
test.pl file.txt debug
-
Would set the flag to 0
I would assume this how you do this, except whatever is inputted, it always drops into the first part of the if and sets the flag to 1.
That would work fine, but you need to use the string comparison,
eq, rather than numeric comparison,==.Also, you can shorten that up to just:
In general, I prefer to use the environment for debug settings, though.
Then you can do things like:
or set the test on for every run in bash or zsh:
or even have more than one debug level if you need aggressive debugging output to help diagnose a particular problem:
and in your code: