develooper Front page | perl.perl6.language | Postings from March 2005

Re: Optional binding

Thread Previous | Thread Next
From:
Aldo Calpini
Date:
March 7, 2005 08:36
Subject:
Re: Optional binding
Message ID:
422C82F8.7080106@perl.it
David Storrs wrote:
> Urk. I, for one, will definitely find this surprising.  I would have
> expected:
> 
>   x = <whatever>; $y = 1; z = 2 3

to obtain what you have expected, you need to explicitly treat the array 
as a list of values with the unary splat:

     foo($x, *@a);

> But I suppose it's all a question of learning to love the Brave New
> World in which arrays are actually objects (sort of).

more to the point, arrays are automagically referenced in scalar context:

     @a = @b;  # same as perl5
     $a = @b;  # means $a = \@b in perl5
     $a = *@b; # means $a = @b in perl5 (more or less)

another thing that may surprise you (it still surprises me, somehow):

     sub bar($x, $y, $z) { ... }
           # ^ note x is scalar here!

     my @coords = (1.0, 3.0, 5.0);
     bar(@coords); # wrong
                   # x => \@coords, y => undef, z => undef

     bar(*@coords); # ok

but then, you could define:

     multi sub bar($x, $y, $z) { ... }
     multi sub bar(@coords is shape(3)) {
         my($x, $y, $z) = @coords;
         return bar($x, $y, $z);
     }

     bar(@coords); # ok now

cheers,
Aldo

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About