Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello monks

I am trying to understand the behavior of Getopt::Long::GetOptions(), in particulat what happens when user did not supply option values. Refering to my script below :

Cases 1..4 make sense.

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

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.

In fact, behavior in cases 5 and 6 is same, while the documentation suggests to me that it would be different.

Can anyone clarify this ?

#!perl -w use strict; use Getopt::Long; use Data::Dumper; $Data::Dumper::Indent = 0; $|++; use Test::More tests => 12; # using scalars for options # The option name as specified to the GetOptions() function is called +the option specification. # Later we'll see that this specification can contain more than just t +he option name. # The reference to the variable is called the option destination. # Case 1 from module synopsis { my $data = "file.dat"; my $length = 24; my $verbose; my $result = GetOptions( "length=i" => \$length, # numeric "file=s" => \$data, # string "verbose" => \$verbose # flag ); ok($result); is( Data::Dumper->Dump( [ $data, $length, $verbose ], [qw / data l +ength verbose /] ), "\$data = 'file.dat';\$length = 24;\$verbose = undef;" ); } # For options that take values it must be specified whether the option + value is required or not, and what kind of value the option expects. # Three kinds of values are supported: integer numbers, floating point + numbers, and strings. # If the option value is required, Getopt::Long will take the command +line argument that follows the option and assign this to the option v +ariable. # If, however, the option value is specified as optional, this will on +ly be done if that value does not look like a valid command line opti +on itself. # Case 2 { @ARGV = (); my $tag = 'deftag'; # option variable with default value my $result = GetOptions( 'tag=s' => \$tag ); ok($result); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'deftag'; +" ); } # Case 3 { @ARGV = ('--tag=MYTAG'); my $tag = 'deftag'; # option variable w +ith default value my $result = GetOptions( 'tag=s' => \$tag ); ok($result); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'MYTAG';" + ); } # Case 4 { @ARGV = ('--bingo --tag=OTHERTAG'); my $tag = 'deftag'; # option variable w +ith default value my $result = GetOptions( 'tag=s' => \$tag ); ok( !$result ); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'deftag'; +" ); } # In the option specification, the option name is followed by an equal +s sign = and the letter s. # The equals sign indicates that this option requires a value. # The letter s indicates that this value is an arbitrary string. # Other possible value types are i for integer values, and f for float +ing point values. # 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 option +s get an empty string '' assigned, while numeric options are set to 0 +. # Case 5 { @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok($result); is( Data::Dumper->Dump( [ $tag, $ival ], [qw / tag ival /] ), "\$t +ag = undef;\$ival = undef;" ); # why not a failure, ok(! $result); } # Case 6 { @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok($result); is( Data::Dumper->Dump( [ $tag, $ival ], [qw / tag ival /] ), "\$t +ag = undef;\$ival = undef;" ); # why not "\$tag = '';\$ival = 0;" } __END__
Rudif

In reply to 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 studying the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found