@a = (1 .. 5);
say @a[1]; # Warning
say @a[1..1]; # No warning
say @a[1,]; # No warning
@b = 1;
say @a[@b]; # No warning
Four one element slices, but only one warns.
Frankly, I find the warning a bit silly; specially the warning if the slice is used in rvalue context. There isn't anything else the programmer could have reasonably meant. And in Perl6, @a[1] is going to be the required syntax anyway. | [reply] [d/l] [select] |
Also, @array[1] drives a list context whereas $array[1] would drive scalar context. Hence:
@array[1] = @another_array;
will behave differently than$array[1] = @another_array;
The former will put the index (i.e., the number of elements in @another_array minus one) of the last element in @another_array in the first element of @array; whereas the second will put the number of elements in @another_array in the first element of @array.Similarly, if you use @array1 as input to a function that behaves differently in list context vs. scalar context, the use of @array[1] will result in list context behavior, whereas $array[1] will result in scalar context behavior (hence, actually, the differences shown above in my example).
| [reply] [d/l] [select] |
func $foo[1], @bar[1]
scalar, or list?
The only difference between $arr[1] and @arr[1] lie in the cases were it can give context: lvalue context. In rvalue context, there isn't one iota of difference (which, IMO, means the warning is utterly bogus if triggered in rvalue context).
There is a difference between $arr[EXPR] and @arr[EXPR], where EXPR isn't a scalar literal; the former gives scalar context to EXPR, the latter gives list context. But just where it makes a difference, Perl remains silent (rightly so, of course).
Considering this is a warning about something that can be determined by a static inspection of the code (@{$arr}[1] doesn't trigger for instance), IMO, such a warning belongs in a linter. Perl::Critic for instance. | [reply] [d/l] [select] |
No, the first one will put the first element of @another_array into @array.
> perl -e '@a=(5,6,7); @array[1] = @a; print @array;'
5
| [reply] [d/l] |