Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Options with optional values and condititions

by runrig (Abbot)
on Oct 02, 2012 at 16:23 UTC ( #996880=note: print w/replies, xml ) Need Help??

in reply to Options with optional values and condititions

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.

Replies are listed 'Best First'.
Re^2: Options with optional values and condititions
by Anonymous Monk on Oct 02, 2012 at 18:47 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996880]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2018-04-19 16:32 GMT
Find Nodes?
    Voting Booth?