Re^3: Is this a bug, or expected behavior?
by merlyn (Sage) on Mar 17, 2006 at 15:39 UTC
|
The context applied to $a->[1..4] does not affect how the subscript is being evaluated, or else we'd go crazy! It's always going to apply scalar context to the subscript, much as $a[EXPR] always applies scalar context to EXPR.
If you want a single element, use the arrow notation. If you want a slice, use the slice notation. That's the way it works.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Why? I mean, I can see that this:
$b = [ $a->[1..4]->[11..12] ];
Would require that 1..4 get evaluated in scalar context, but why would 11..12 also need to be evaluated in scalar context? Why doesn't the last thing in the -> chain inherit the context from the overall chain?
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Does "by definition" and "it makes sense to Larry" help here?
The index of a scalar access to a data structure (array or hash) is always evaluated in scalar context, because one and only one item comes back from the calculation.
You don't expect $a->[3..5] to come back with 3 items, do you?
If so, please reset your expectations to the way Larry implemented that design. If you want a slice, there's a slice notation. If you want a single element, you have a single element notation.
Note that for Perl6, Larry changed his mind ("see rule #1"), and it acts differently, which I think will mean that DWIMmery will eventually lead to unpredictable behavior at that level. In fact, I saw some evidence of that in a Perl6 design discussion two years ago.
However, for Perl5, it's very predictable, and doesn't depend on the context in which the item appears, which I think is quite nice.
| [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Re^3: Is this a bug, or expected behavior?
by Fletch (Bishop) on Mar 17, 2006 at 15:37 UTC
|
No, you're dereferencing a single element of an array reference. Dereferencing a slice from an array reference would, though.
$ cat wa
sub ctx {
my $w = wantarray;
if( defined $w ) {
print $w ? "array\n" : "scalar\n";
} else { print "void\n" }
1;
}
my $a = [ qw( a b c ) ];
$a->[ ctx( ) ];
@{$a}[ ctx( ) ];
$ perl wa
scalar
array
| [reply] [Watch: Dir/Any] [d/l] |
Re^3: Is this a bug, or expected behavior?
by xdg (Monsignor) on Mar 17, 2006 at 15:51 UTC
|
The [1..4] in list context is used with slices -- it doesn't create a list context on its own. Because you ask for $a->, you're telling Perl to dereference $a to a single element, not a slice, thus 1..4 is interpreted in scalar context.
In the case that works, you're dereferencing $a to an array, and then taking a slice of that -- and because you're slicing, 1..4 gets interpreted in list context.
$a->[1..4]; # not a slice
@{$a}[1..4]; # slice
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re^3: Is this a bug, or expected behavior?
by ikegami (Patriarch) on Mar 17, 2006 at 16:51 UTC
|
Unlike @{$a}[1..4], $a->[1..4] is not a array slice. This table should help you. | [reply] [Watch: Dir/Any] [d/l] [select] |