Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need 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 = ...;.


In reply to Re: Trying to understand subtleties of Getopt::Long, please help by ikegami
in thread Trying to understand subtleties of Getopt::Long, please help by Rudif

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2023-12-11 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?