I have decided to learn Perl as my next language and I am currently reading about the scalar and list contexts, although this question is not specifically Perl-related, I will just put it in a Perl context (hah, get it?!)
So, the basic idea is that operators expect either a scalar or a list value, and if they get an argument that is of a different type than the one they are expecting, they will convert it to the appropriate context.
A couple of examples of the correct contexts being used:
# Scalar context
$first_number = 2;
$second_number = 3;
say 3+2; # This obviously prints 5
# List context
@array = qw (one two three);
@array = reverse @array;
say @array; # Prints threetwoone
Now, if you mix contexts, you can get something like this:
$first_number = 2;
@array = qw (one two three);
$result = 2 + @array;
say $result; # This prints 5
This is because using an array in a mathematical operation returns the length of the array instead.
Now, the question is: why would you ever want to do this unless you are an evil person and you want to make people reading your code miserable? Would it be too mainstream to use something like 2 + $#array instead?
In general, is it good programming practice to let the language take care of this type of scenario? Wouldn't it be better if the programmer took care of the variables and prepared data so that all operators received the correct argument type in the first place?
$#array+1there, I was writing in a hurry. And ok for the type-conversion tag, it was the closest tag I could find :P Your answer makes sense, but doesn't this whole idea of "let's give you what you probably want" make everything more difficult to read, albeit faster to write, especially if you come from other languages where you have to make more stuff explicit? Up to the reader I suppose, but still... – user1301428 Aug 06 '15 at 09:51