Hello,
I have a subroutine where I want to pass some paramaters to it, but assign
default ones if a paramater is not passed. This is similar to CGI.
&mySub( -param1=>'my value', -param3=>'value' ); # -param2 would get a default
value
The problem I am running into is if one of the paramaters passed is an array,
and the value passed is 0, the default value gets assigned instead of the 0 that was
passed. Below is some code that shows what I am doing and how it breaks:
#!/usr/bin/perl -Tw
use strict;
&Baz( one=>'all good', two=>['zero','one'] ); # text works
&Baz( one=>'bad', two=>[0,1] ); # numbers fail
&Baz( one=>'more bad', two=>['0','1'] ); # quoted numbers fail
sub Baz {
my %params = @_;
$params{one} = "default" unless $params{one};
$params{two}[0] = "default" unless $params{two}[0];
$params{two}[1] = "default" unless $params{two}[1];
# let's try some more, they still fail...
$params{two}[0] = "default" unless defined($params{two}[0]);
$params{two}[0] = "default" unless $params{two}[0] || $params{two}[0] == 0;
print "one is $params{one}\n";
print "two[0] is $params{two}[0]\n";
print "two[1] is $params{two}[1]\n";
print "\n";
return(1);
}
__END__
$ ./test.array.passing.pl
one is all good
two[0] is zero
two[1] is one
one is bad
two[0] is default
two[1] is 1
one is more bad
two[0] is default
two[1] is 1
I am thinking the unless is failing, but I can't find out why, so I come to
the gurus...
Thanks for your time,
=-= Robert Thompson
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]