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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.