> In Perl, there are three contexts in which an expression may be evaluated.
> 1. scalar
> 2. array
> 3. void
There's actually no such thing as "array context" in Perl; instead there's "list context". An array is a list that's been stored in a variable (this is a fairly common mistake).
See http://friedo.com/blog/2013/07/arrays-vs-lists-in-perl and http://perlmaven.com/scalar-and-list-context-in-perl for good examples/discussion.
EDIT:
Posted this before I finished the article. Understanding the difference between arrays and lists makes the following potential WTFs a lot clearer:
sub take_two_arrs (\@\@) {
print $_[0], $_[1] ;
}
take_two_arrs @a1, @b1 ; # prints ARRAY(0xAddr) ARRAY(0xAddr)
take_two_arrs ((1,2),(3,4)) ; # error: arrays must be named
The second doesn't work because the prototyped function takes
array references, not lists. It would work if you called it like this:
take_two_arrs ([1,2],[3,4])
I'll admit that this is baffling.
sub what_are (++) {
print $_[0], " ", $_[1] ;
}
what_are ((1,2),(3,4)) # prints 2, then 4
(This is part of the reason that Perl programmers don't use prototypes very often.) perlsub warns:
> When using the + prototype, your function must check that the argument is of an acceptable type.
The plus here forces scalar context on the arguments, which are lists (not arrays!), so they return their last elements. This would work how the author probably wants if called like this:
what_are ([1,2],[3,4]); # prints ARRAY(0xAddr), ARRAY(0xAddr)