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

little addendum:

> Note that @{$res->{objects}} and @$res->{objects} are not the same.

... b/c of Perl's precedence rules:

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

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^3: Referring to an array without copying to a separate variable first
by kcott (Archbishop) on Jun 27, 2013 at 09:33 UTC
    "@$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

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

Re^3: Referring to an array without copying to a separate variable first
by Anonymous Monk on Jun 27, 2013 at 00:06 UTC

    which doesn't make sense.

    Sure it does, $res just happens to be a hash

      > Sure it does, $res just happens to be a hash

      No it never does, since the code @{$res}->{objects} tries to dereference a hash-ref not an array.

      But interestingly this magically (?) works:

      DB<112> @a="a".."d" => ("a", "b", "c", "d") DB<113> @a->[2] => "c"

      aaaah! =)

      DB<114> use warnings; @a->[2] Using an array as a reference is deprecated ...

      Cheers Rolf

      ( addicted to the Perl Programming Language)