On 30 July 2013 19:54, gvim <gvi...@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'

Reply via email to