Assigning anonymous hash to a list

Hakim Cassimally hakim.cassimally at gmail.com
Tue Jul 30 20:23:40 BST 2013


On 30 July 2013 19:54, gvim <gvimrc at gmail.com> 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.


It's not inconsistent.  Just because your language can do assignment
doesn't mean it can do destructuring bind.

As it happens, Perl *can* do destructuring bind.  You want parentheses
around the list though:

    my ($str, $ref) = ('text', {a => 1, b => 2, c => 3});
    say $ref->{b};

(Your line parses the hash reference in void context, as you'd see if
you're using warnings.)

osf'


More information about the london.pm mailing list