Just another Perl shrine | |
PerlMonks |
Re^3: What is the difference between $array[1] and @array[1]?by JavaFan (Canon) |
on Apr 09, 2009 at 14:07 UTC ( [id://756608]=note: print w/replies, xml ) | Need Help?? |
Similarly, if you use @array[1] 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 behaviorWhat kind of rubbish statement is that? The scalar vs list behaviour of a function is determined by its context, not the sigils of its arguments. Suppose you were right, what behaviour would func have below: 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.
In Section
Seekers of Perl Wisdom
|
|