in reply to Re^2: Referring to an array without copying to a separate variable first
in thread Referring to an array without copying to a separate variable first

"@$res->{objects} means @{$res}->{objects} which doesn't make sense."

It doesn't make sense to me either but B::Deparse seems to know what's going on:

$ perl -MO=Deparse,p -e '@{$res}->{objects}' @{$res;}->{'objects'}; -e syntax OK

I suspect the ; after $res is the key but I'm not sure why. I shall have to give this some more thought.

-- Ken

Replies are listed 'Best First'.
Re^4: Referring to an array without copying to a separate variable first
by LanX (Saint) on Jun 27, 2013 at 10:04 UTC
    > ... but B::Deparse seems to know what's going on: ...

    B::Deparse can't help here, cause it's a runtime error and not a syntax problem.

    > ... I suspect the ; after $res is the key ...

    The ';' you are seeing is an indicator that %{ } and alike are effectively do { } blocks plus dereferencing.

    lanx@nc10-ubuntu:~$ perl -e '$r={a=>1};print %{$r;},"\n"' a1 lanx@nc10-ubuntu:~$ perl -MO=Deparse,p -e '$r={a=>1};print %{$r;},"\n" +' $r = {'a', 1}; print %{$r;}, "\n"; -e syntax OK lanx@nc10-ubuntu:~$ perl -MO=Deparse,p -e 'print %{ $r={a=>1};$r; },"\ +n"' print %{$r = {'a', 1}; $r;}, "\n"; -e syntax OK lanx@nc10-ubuntu:~$ perl -e 'print %{ $r={a=>1};$r; },"\n"' a1

    Interesting, well spotted! =)

    UDPATE

    Reminds me of this old trick to have code executed within interpolation

    DB<100> print " @{ [ 1..10 ] } " 1 2 3 4 5 6 7 8 9 10

    maybe clearer

    DB<106> print " @{ $a++; [1..$a ] } " 1 DB<107> print " @{ $a++; [1..$a ] } " 1 2 DB<108> print " @{ $a++; [1..$a ] } " 1 2 3

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Just noticed your update. I don't know how old that trick is but I do use it quite often — most recently in the last hour for checking a subroutine's arguments:

      my (..., $some_scalar_ref, ...) = @_; say "... @{[$$some_scalar_ref // '<undef>']} ...";

      Anyway, thanks for the feedback. ++

      -- Ken