Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Options with optional values and condititions

by Qukz (Novice)
on Oct 02, 2012 at 16:10 UTC ( #996878=perlquestion: print w/ replies, xml ) Need Help??
Qukz has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I'm trying to use an option with an optional value and then test whether the option was used and if so whether an optional value was used with it; here's what I have:

use strict; use warnings; my $foo = undef; GetOptions( 'foo:s' => \$foo, ); my $sn = $ARGV[0]; if ($foo and $foo ne undef) { print "foo option used with optional value: $foo \n"; } elsif ($foo) { print "foo option used with no optional value \n"; } else { print "foo option not used at all \n"; }

That doesn't work, but hopefully it better illustrates what I'm trying to do.

Any help would be greatly appreciated.

Comment on Options with optional values and condititions
Download Code
Re: Options with optional values and condititions
by kennethk (Monsignor) on Oct 02, 2012 at 16:14 UTC
    I think the logic you mean is:
    use strict; use warnings; use Getopt::Long; my $foo = undef; GetOptions( 'foo:s' => \$foo, ); my $sn = $ARGV[0]; if (not defined $foo) { print "foo option not used at all \n"; } elsif (not length $foo) { print "foo option used with no optional value \n"; } else { print "foo option used with optional value: $foo \n"; }

    I used the length test because 0 is a potentially valid input but evaluates to false in logical context. This would also hold for a whitespace-only input, e.g.:

    perl script.pl --foo='   '


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Options with optional values and condititions
by runrig (Abbot) on Oct 02, 2012 at 16:23 UTC
    What I think you're looking for is:
    if ($foo) { print "foo option used with optional value: $foo \n"; } elsif (defined $foo) { print "foo option used with no optional value \n"; } else { print "foo option not used at all \n"; }
    But IMHO, 'options with optional values' are too ambiguous and I've never seen a need for such, so if you are going to have an option, you should either have a required value or no value at all.
      Careful ... if ($foo) probably-unintentionally checks for truth, such that a value of zero would fail the test.
        if ($foo) probably-unintentionally checks for truth...
        It was intentional by me, though I don't know what the OP wants. From the docs:
        Using a colon ":" instead of the equals sign indicates that the option value is optional. In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while numeric options are set to 0.

        So yes, if you supply a value of "0" to a string option, you'll miss it. If you want to catch that situation you'll have to code for it. I often assume such strings are invalid in most practical situations.

Re: Options with optional values and condititions
by Neighbour (Friar) on Oct 03, 2012 at 13:29 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996878]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-16 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (9 votes), past polls