in reply to Re^4: Is this a bug, or expected behavior?
in thread Is this a bug, or expected behavior?

It does because it is a scalar context.

Your problem is that you're not accepting/catching/groking that the parts inside the [] have their own context due to their nature (dereferencing a single element from an array reference) that's independent from the context of the larger expression. It doesn't matter how many of them you stack up, each of them is trying to retrieve a single element. Slices use @{ $aref }[ LIST ], single elements use ${ $aref }[ EXPR ]. The later is not the former, the former is not the later. They're two different things which impose different contexts on the subscripts.

Update: let me rewrite your example using the more explicit brace-y syntax I used; that might help clear it up. Your:
$a->[1..4]->[11..12]

is the same as:
${ ${ $a }[ 1..4 ] }[ 11..12 ]

A similar slice (which due to the indices you used wouldn't make much sense :) would be:
@{ @{ $a }[ 1..4 ] }[ 11..12 ]

But again, you don't have a slice, you're fetching a single element so it's scalar context inside the []s.

Replies are listed 'Best First'.
Re^6: Is this a bug, or expected behavior?
by fizbin (Chaplain) on Mar 17, 2006 at 17:20 UTC

    Okay, that now makes sense.

    What actually helped shed light on it was using your ctx function in this code:

    sub ctx { my $w = wantarray; print "$_[0]:"; print ( defined $w ? ($w?'array':'scalar') : 'void'); print "\n"; \&ctx; } sub ctxind { my $w = wantarray; print "$_[0]:"; print ( defined $w ? ($w?'array':'scalar') : 'void'); print "\n"; 1; } my $a = [ ctx("buildA1"),ctx("buildA2"),ctx("buildA3"),ctx("buildA4") +]; ctx("by itself"); @{$a}[ctxind("slice ndx")]; $a -> [ctxind("plain ndx")] -> ("fn1",ctx("args1"),ctx("args2")) -> ("fn2",ctx("args3"));
    This yields:
    buildA1:array buildA2:array buildA3:array buildA4:array by itself:void slice ndx:array args3:array args1:array args2:array plain ndx:scalar fn1:scalar fn2:void

    Which now all makes sense, except for the "args3" being evaluated before "args1". That seems like a rather bizarre evaluation order.

    The contexts inside () on the function references are all array context, and the context inside [] is array or scalar depending only on the immediately surrounding syntax. The context of the function called by the dereference itself depends on the surrounding code.

    I guess if TIEARRAY worked better, I'd be able to tie the array that $a points to and see that the FETCH method was getting called with different contexts depending on the code surrounding the array ref, even though the context applied to the expression generating the index was determined only by wheter I'd used the slice syntax or not. Unfortunately, it appears that FETCH is always called in scalar context, no matter what.

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/