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?


In reply to Detecting an undefined hash key by LesleyB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.