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

> ... 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)

Replies are listed 'Best First'.
Re^5: Referring to an array without copying to a separate variable first
by kcott (Archbishop) on Jun 30, 2013 at 05:07 UTC

    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