2 questions:
1: why does this not work:
The output isprint "not ref:\n"; test(dogs=>'bark', cats=>'miaow'); print "ref:\n"; test({dogs=>'bark', cats=>'miaow'}); sub test{ my $params; ref($_[0]) ? $params = shift : $params = {@_}; print Dumper($params); }
Which would seem to imply that the test is always false, but if I change $params = {@_} to print "hello" I get:not ref: $VAR1 = { 'cats' => 'miaow', 'dogs' => 'bark' }; ref: Odd number of elements in anonymous hash at params.t line 16. $VAR1 = { 'HASH(0x81194e0)' => undef };
So in this case, when it is a reference, the test correctly evaluates to true and doesn't print 'hello'not ref: hello $VAR1 = undef; ref: $VAR1 = { 'dogs' => 'bark', 'cats' => 'miaow' };
Also, if I do it like:
I get exactly what I was expecting:if(ref($_[0])){ $params = shift; } else{ $params = {@_}; }
?????not ref: $VAR1 = { 'cats' => 'miaow', 'dogs' => 'bark' }; ref: $VAR1 = { 'cats' => 'miaow', 'dogs' => 'bark' };
2. Is there a better way to allow a sub to be called like either test(dogs=>bark, cats=>miaow) or test({dogs=>bark, cats=>miaow})?
Cheers, Cxx
Edit by BazB retitle from ":? wierd"
In reply to Parameter list wierdness by CassJ
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |