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

I get different command-line parm results using the functions getopt() vs getopts(). The doc I have read only mentions that getopts() will warn the user on unknown switches.

In the sample code below, the output will change depending on wheather you're using getopt() or getopts().

use strict; use Getopt::Std; our ($opt_h, $opt_a); getopt('ha'); print "help=$opt_h\n" if (defined $opt_h); print "all=$opt_a\n" if (defined $opt_a);

Results with getopt

perl -w test.pl -h -a help=-a

Results with getopts

perl -w test.pl -h -a help=1 all=1

So, at this point I know how to get what I want (use getopts). However, being a perl newbie and in learn mode, I am curious about this behavior.

Could this be a platform issue?

Replies are listed 'Best First'.
Re: Getopt: vs. Getopts confusion
by shenme (Priest) on Aug 30, 2003 at 23:26 UTC
    I think I found the important sentence in the description for getopt():
        "Pass one argument which is a string containing
         all switches that take an argument."
    

    So you can give 'ha' to getopts() and it will understand that these options don't take arguments.   To do the same for getopt() you don't mention those options...?   As it was, your mentioning 'h' to getopt() meant that getopt() ate up the next 'value' ('-a') for the option.

    If I read it right then getopts('oif:') is matched by using getopt('f').

      OK, that all makes sense now.

      After reading the doc more carefully and playing with a test script, the difference is apparent... I must have just glossed right over that little point.

      Thanks