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

I have just begun using the Getopt::Long CPAN module, and I'm a bit confused about its return result. For example:
use strict; use warnings; use Getopt::Long; { my $blah; my $result = GetOptions('blah=i' => \$blah); print "result = $result\n"; print "blah = $blah\n"; }
By my understanding, blah is a required integer value, but when i invoke my script without a blah arg, i still get a true result:

perl -w getopt_test.pl
result = 1
Use of uninitialized value in concatenation (.) or string at getopt_test.pl line 12.
blah =

It makes sense that $blah is uninitialized, but i thought that $result would be false.

When i invoke this with a blah option, i get a true result also:

perl -w getopt_test.pl --blah 4
result = 1
blah = 4

As a side note:
By my understanding, i dont need to say --blah=4, i can say --blah 4, and that seems to parse correctly by my test runs.

So what am i missing here?

Replies are listed 'Best First'.
Re: Getopt::Long result issue
by Sidhekin (Priest) on Sep 07, 2004 at 16:03 UTC

    By my understanding, blah is a required integer value, but when i invoke my script without a blah arg, i still get a true result

    What is "required" here is the integer value, not the parameter itself. That is, specifying blah without specifying a value will fail:

    perl -w getopt_test.pl --blah Option blah requires an argument result = Use of uninitialized value in concatenation (.) or string at getopt_te +st.pl line 9. blah =

    Quoting from the documentation ...

    GetOptions does not return a false result when an option is not supplied

    That's why they're called 'options'.

    ... which says it all really ... :-)

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Getopt::Long result issue
by Random_Walk (Prior) on Sep 07, 2004 at 16:07 UTC
    When Getopt::Long says "requires a value" what it says is that if blah occurs it must have a value, not that blah is required. If you run your script with -blah (no value given) you will see $result is undef.

    I am afraid what you will have to do is check blah is set once you have eaten up the options.

    use strict; use warnings; use Getopt::Long; { my $blah; my $result = GetOptions('blah=i' => \$blah); &usage unless $blah; print "result = $result\n"; print "blah = $blah\n"; } sub usage { print "$0: -blah=value\n" }

    Cheers,
    R.
Re: Getopt::Long result issue
by herveus (Prior) on Sep 07, 2004 at 16:06 UTC
    Howdy!

    Sidhekin covered the direct answer just fine.

    If you want to make 'blah' a mandatory option, just test $blah for definedness after the call to GetOptions, just as you would validate any other options for the right combinations or existence.

    yours,
    Michael
Re: Getopt::Long result issue
by Zaxo (Archbishop) on Sep 07, 2004 at 16:13 UTC

    "Required" in the Getopt::Long docs means that a value is required, not that the option's use is required. If you want that, call die 'Foo requires blah be specified' unless defined $blah; after the GetOptions call. GetOptions returns undefined if options are misused or unknown options are given.

    After Compline,
    Zaxo

Re: Getopt::Long result issue
by perlfan (Parson) on Sep 07, 2004 at 18:15 UTC
    If you want to test if something was passed, you need to test the reference that the options affects. GetOptions doesn't return anything of use.
    #!/usr/bin/perl -w use strict; use Getopt::Long; my $fVal = 0; GetOptions( f => \$fVal, ) # if "-f" is used, this will print "1" else "0" print "$fVal\n";