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