Evaluating an array in scalar context yields a count of how many elements exist in the array. Lists are different. People will jump in now and say there's no such thing as a list in scalar context. There isn't. So consider this code:
perl -e "print scalar( 'a', 'b', 'c', 'd' );"
The output is 'd'. perlop explains why:
Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator.
In list context, it's just the list argument separator, and inserts both its arguments into the list.
The same is true for slices of an array, and lists generated with the qw// operator or through other basic means.
List functions can be defined to do all manner of different things in scalar context. split tells you the number of elements created, and splits into @_. Other functions do other things. That's dependant entirely on the function's code.
The diamond operator is defined to slurp a file in list context, or to return the next line (starting with the first) in scalar context.
So I guess that the answer is, if you got the last element you got documented behavior for some operators. If you're reading a single line from a filehandle, you'll get the first line the first time you read from the filehandle. That too is expected, and documented in perlop. If you got something else, we'll need to see more information to explain where that behavior is documented.
|