I am posting this because I think I finally get it, and I want to check with the senior monks to see if I'm right. Also, I want other monks to share in my epiphany. :)
When I first started learning Perl, I had a heck of a time distinguishing between scalar and array context. After I got that distinction, I had an even harder time figuring out the difference between array and list context. Here's some code that demonstrates the differences:
my @array = ('foo', 'bar', 'baz');
$temp = @array;
$temp = ('foo', 'bar', 'baz');
($temp) = @array;
($temp) = @array[0..2];
$temp = @array[0..2];
Think these all print the same thing? Think again. On running the above script, you will get
as your output. Here's why:
- This assignment has a scalar on the left hand side. This forces the array to be evaluated in a scalar context, which gives the length of the array.
- This assignment has a scalar on the left hand side. This forces the list to be evaluated in a scalar context, which gives the last element of the list.
- This assignment has a list on the left hand side. @array is thus evaluated in an array context. When Perl sets assigns one array to another, it sets the first element of one array equal to the first element of the other array, the second element equal to the next element, etc. Extra values are thrown away, so $temp is now equal to the first value of @array, or 'foo'.
- This assignment also has a list on the left hand side, and an array slice (containing all the elements of the original array) on the right. The behavior is the same as in 3.
- This assignment has a scalar on the left and the array slice (a.k.a. a list) on the right. This looks like a combination of 1. and 4., so you would think that the answer would be 3 or 'foo'. It turns out to be 'baz', because lists assigned to scalar context return the last element of the list.
So my current understanding is this:
- The difference between a list and an array is that an array is an allocated chuck of memory, whereas a list is a bunch of scalars that are the result of an expression.
- Arrays and lists act the same in list context.
- Arrays and lists act very differently in scalar context; the former return their size, and the latter return their last element.
This last difference can be very subtle; for instance, these two subroutines do NOT return the same value (set their results equal to scalars to see how):
return('foo', 'bar' 'baz');
my @array = ('foo', 'bar', 'baz');
Is my understanding right? Anything else about lists that I should know about?
Be bloody, bold, and resolute; laugh to scorn
The power of man...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||