You have omitted one key piece of information: how are you calling your program?

For example, do you call it with "-t" or with "-t 1"? There is a difference.

I have added some print statements to your code to help debug:

> cat 714766.pl #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use Getopt::Std; #print 'ARGV ',Dumper(\@ARGV); my $test = 1; my %opt; getopt('st',\%opt); #print 'opt ',Dumper(\%opt); if ($opt{'t'}) { $test = 1; ## testing #print "set test=1 again\n"; } elsif ($opt{'s'}) { $test = 0; ## not testing #print "set test=0\n"; } #print "test=$test\n"; if ( (!(defined($opt{'s'}))) && (!(defined($opt{'t'}))) ) { print "neither -t or -s used\n"; } else { print "either -t or -s used\n"; }

Now some sample program calls (5.8.8 on linux):

> ./714766.pl neither -t or -s used > > ./714766.pl -t neither -t or -s used > > ./714766.pl -t 0 either -t or -s used > > ./714766.pl -t 1 either -t or -s used > > ./714766.pl -s 5 either -t or -s used

Data::Dumper can help you understand what is in %opt

Update: It seems to me that the documentation for Getopt::Std is incorrect:

getopt() and getopts() will also accept a hash reference as an optional second argument. Hash keys will be x (where x is the switch name) with key values the value of the argument or 1 if no argument is specified.
If no argument is specified for getopt, the hash key is NOT set to 1 (Dumper shows it as 'undef').

In reply to Re: Detecting an undefined hash key by toolic
in thread 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.