Ah, right. Okay, so I can see why scalar(1..4) is ''; now my question is, why is 1..4 getting a scalar context above when I assign to $b? Shouldn't [ $a->[1..4] ] cause 1..4 to be evaluated in list context?
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/
| [reply] [d/l] [select] |
| [reply] [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] [d/l] [select] |
$ 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] [d/l] |
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] [d/l] [select] |
Unlike @{$a}[1..4], $a->[1..4] is not a array slice. This table should help you.
| [reply] [d/l] [select] |