This is another section that I am just copying from an earlier page but I thought it was worth repeating in the context of "traps".
You might have noticed that I make a distinction of talking about arrays and lists, and you may wonder, "What's the difference?" Well, they are very very similar. And to this day, I still have trouble telling them apart. This is one of the unfortunate things in Perl. Now, if you're disciplined about accessing scalar return values of subroutines into scalar values and array/list return values of subroutines into arrays, you will stay out of trouble. The trouble occurs when you try to implicitly cast arrays and lists return values to scalars.
This is discussed in Programming Perl [WALL00] in the 3rd edition, pp 72-74. But here's where it gets confusing:
# LIST context
$operation = ('generateRIB', 'renderRIB', 'cleanupRIB');
# got 'cleanupRIB'
# ARRAY context - $operation will be the scalar context of the array.
# This will actually return the number of elements in the array
# instead of giving you any value in the array. So $operation
# will be 3 ($operation didn't get an operation at all)
@operations = ('generateRIB', 'renderRIB', 'cleanupRIB');
$operation = @operations;
# got 3
# List assignment - As if this wasn't confusing enough, if the
# left hand side of the assignment is a list, the values will be
# assigned in the order listed. So...
($operation) = ('generateRIB', 'renderRIB', 'cleanupRIB');
# got 'generateRIB'
@operations = ('generateRIB', 'renderRIB', 'cleanupRIB');
($operation) = @operations;
# also got 'generateRIB'
You can probably understand why I find this confusing. But as we see in the
last example, if we receive the array or list into a list context, then we
are pretty safe, and we know that we are receiving the first value into the
scalar. This is actually pretty useful in receiving paramenters in a
subroutine. If you return values from an array you should receive the same
type you return. That is, receive scalars into scalars and arrays/lists
into lists.
There's actually one more convenience. On the left hand side, if you include an array as the last element in the list, it will absorb all the rest of the elements:
@operations = ('generateRIB', 'renderRIB', 'cleanupRIB');
($operation, @remainingOperations) = @operations;
# $operation got 'generateRIB'
# @remainingOperatiiions got ('renderRIB', 'cleanupRIB')
I guess the only other thing I would add is: Live with it. I honestly don't get stung by this one much myself. But I do try to avoid it by not doing any implicit casting from and array/list to a scalar.
Now that I've gone over that, I'm going to refer to arrays and lists interchangably, usually as "arrays."