in reply to Re^3: Detecting an undefined hash key
in thread Detecting an undefined hash key

toolic, thank you for taking the time on this.

I did cut and paste your code and uncommented the usage of Data Dumper to see what was happening.

Using -t, I get

./714799.pl -t opt $VAR1 = { 't' => undef }; neither -t or -s used

with similar results for a singular -s.

Observe the test to see if either option had been specified fails.

Using both -s and -t I get

./714799.pl -s -t opt $VAR1 = { 's' => '-t' }; either -t or -s used

Using -a -b gives

./714799.pl -a -b opt $VAR1 = { 'a' => 1, 'b' => 1 }; neither -t or -s used

and singletons of either give one hash element with the value one.

So I still feel there is something strange about at least s and t as options for getopt.

I read the documentation and saw that getopt and getopts appear to behave differently.

It is mea culpa because the documentation clearly states getopt expects each switch to have an argument. Therefore -s -t should interpret -t as the argument to the -s switch (and vice versa).

Except this doesn't appear to hold if one uses -a -b. Here the default value of 1 is used for both.

I may try checking the rest of the alphabet on this one

The end result is the getopts function behaves the way I originally expected getopt to behave

Thank you once again for taking the time on this

Update

./714799.pl -a -b -c -d -e -f -g -h -i -j -k -l -m -n -o -p -q -r -s - +t -u -v -w -x -y -z opt $VAR1 = { 'w' => 1, 'r' => 1, 'a' => 1, 'x' => 1, 'd' => 1, 'j' => 1, 'y' => 1, 'u' => 1, 'k' => 1, 'h' => 1, 'g' => 1, 'f' => 1, 'i' => 1, 'e' => 1, 'n' => 1, 'v' => 1, 'm' => 1, 's' => '-t', 'l' => 1, 'c' => 1, 'p' => 1, 'q' => 1, 'b' => 1, 'z' => 1, 'o' => 1 }; either -t or -s used

and

./714799.pl -a -b -c -d -e -f -g -h -i -j -k -l -m -n -o -p -q -r -s - +u -t -v -w -x -y -z opt $VAR1 = { 'w' => 1, 'r' => 1, 'a' => 1, 'x' => 1, 'd' => 1, 'j' => 1, 'y' => 1, 'k' => 1, 'h' => 1, 'g' => 1, 'f' => 1, 't' => '-v', 'i' => 1, 'e' => 1, 'n' => 1, 'm' => 1, 's' => '-u', 'l' => 1, 'c' => 1, 'p' => 1, 'q' => 1, 'b' => 1, 'z' => 1, 'o' => 1 }; either -t or -s used

-s and -t appear to be hungry in getopt

Replies are listed 'Best First'.
Re^5: Detecting an undefined hash key
by pjotrik (Friar) on Oct 01, 2008 at 23:43 UTC
    OK, again:

    There only is "something strange" about s and t, because you tell getopt to read values for these switches, calling it getopt('st'). The parameter for getopt doesn't contain "allowed" switches, it contains switches that expect an argument.

    If you don't need values for these switches (which seems to be the case) just don't name them in the first parameter to getopt. Getopt will still, when it stumbles upon that switch on the command line, define the hash entry for the given letter and give it a value of 1.

      I'm a numpty

      Thank you for being so patient