Hi,
Uri Guttman wrote:
>>>>>> "IB" == Ingo Blechschmidt <[EMAIL PROTECTED]> writes:
> IB> * "(key => $value)" (with the parens) is always a positionally
> passed
> IB> Pair object. "key => $value" (without the parens) is a named
> IB> parameter:
>
> IB> sub foo ($a) {...}
>
> IB> * Unary "*" makes a normal pair variable participate in named
> binding:
>
> IB> foo(*$pair); # named parameter "a", $a will be 42
>
> IB> * Same for hashes:
>
> IB> my %hash = (a => 1, b => 2, c => 3);
>
> IB> foo(%hash); # positional parameter, $a will be \%hash
>
> IB> foo(*%hash); # three named parameters
>
> IB> Opinions?
>
> works for me.
Great! :)
> but what about lists and arrays?
>
> my @z = ( 'a', 1 ) ;
> foo( @z ) # $a = [ 'a', 1 ] ??
Yep.
> my @z = ( a => 1 ) ;
> foo( @z ) # $a = pair( a => 1 ) or does that need * too?
No. Even foo([EMAIL PROTECTED]) would cause $a to be the Pair (a => 1).
If you really wanted to have an array of pairs participate in named
binding, you'd have to write:
foo(*hash(@z)); # (which is short for)
my %hash = @z; foo(*%hash);
> same questions for lists (this shows a nested sub call)
>
> sub bar { return ( a => 1 ) }
> foo( bar() ) # i would expect $a == ( a => 1 ) since there is
> # no *
Yep.
> foo( *bar() ) # i would expect $a == 1
Yep.
--Ingo