Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Trying to understand subtleties of Getopt::Long, please help

by ikegami (Patriarch)
on Sep 23, 2007 at 22:18 UTC ( [id://640617]=note: print w/replies, xml ) Need Help??


in reply to Trying to understand subtleties of Getopt::Long, please help

Case 5, since the options are required ('='), why does GetOptions() return true? I expected it to fail.

You used : where you meant to use =, but that doesn't change the result.

The *option's argument* is required, not the option itself.

If you want to make the option required, you can check if the option variable is defined after the call to GetOptions.

Case 6, since the options are not required (':'), and the documentation says "In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while numeric options are set to 0.", I expected '' and 0 to be asigned to the variables.

It's a bit clearer in the documentation of for ":": "If [the option's argument] is omitted, an empty string will be assigned to string values options, and the value zero to numeric options."

The following would be even clearer: "If the option is present but the the option's argument is omitted, an empty string will be assigned to string values options, and the value zero to numeric options."

Since the option was never specified, its argument was never omitted, so the option variable was never assigned the default value.

You can assign to the option variable before calling GetOptions to get the behaviour you desire.

PS — It's safer to always localize changes to global variables. local @ARGV = ...;.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://640617]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-25 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found