http://www.perlmonks.org?node_id=714766

LesleyB has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I would like to either use some options to set a value or set that value according to the value in a particular file

I start off by setting the default value

my $test = 1;

Using Getopt::Std and a my %opt; hash I then check for two single letter options, s or t with the latter superceding the former.

getopt('st',\%opt); if ($opt{'t'}) { $test = 1; ## testing } elsif ($opt{'s'}) { $test = 0; ## not testing }
I then try to detect whether either of the hash values are defined.
if ( (!(defined($opt{'s'}))) && (!(defined($opt{'t'}))) ) { ## neither option was used let's check sitetest file for a test valu +e printf "Reading from sitetest file\n"; open(TEST,"< test") or die "Can't open file test \n"; my $line = <TEST>; close STEST; print $line."\n"; if ($line =~ /0/) {$test = 0;} }

which isn't working .. the program continues to read from file. Is this because I am using a hash and the second I mention a hash key the key exists or something else? How could I get this to work?

I also don't like the (!(defined($anyvarhere))) construct. Is there a better way to detect the non-existence of a hash key in particular and, in general terms, the undefinedness of a variable?