Assigning anonymous hash to a list

Abigail abigail at abigail.be
Tue Jul 30 20:40:08 BST 2013


On Tue, Jul 30, 2013 at 08:15:06PM +0100, Peter Flanigan wrote:
> On 30/07/13 19:54, gvim wrote:
> > Can anyone explain why this works:
> > 
> > my $ref = {a => 1,  b => 2, c => 3};
> > say $ref->{b};     #  Result: 2
> > 
> > ... but this doesn't :
> > 
> > my ($str, $ref) = 'text', {a => 1, b => 2, c => 3};
> > say $ref->{b};   # Result: Use of uninitialized value
> > 
> > Seems a little inconsistent.
> > 
> > gvim
> > 
> > 
> 
> Try
> 
>    my ($str, $ref) = ( 'text', {a => 1, b => 2, c => 3} );
> 
> The extra set of brackets force evaluation in a list context


List context is caused (indirectly) by the parens on the LHS of the
assignment. The added parens on the RHS do diddly squat for context.
They're there for *precedence*. Their act is played at parse time,
and they don't come back for an encore at compile time, nor run time.


Abigail


More information about the london.pm mailing list