What's the difference between arrays and lists?

One of the most common sources of confusion for new Perl programmers is the difference between arrays and lists. Although they sometimes look similar, they are very different things, and many bugs and misunderstandings are caused by not having a full understanding of the differences. Even experienced Perl programmers sometimes think arrays and lists are the same, but they are quite different in several important ways.

Read more at my blog: Arrays vs. Lists in Perl: What's the Difference?

I'd also love to hear if you can think of any examples that I didn't cover.


Perl arrays vs. lists is a topic on which even the most authoritative and articulate experts often become murky. This is the best writeup of the differences I can remember.

While its true that arrays cannot be returned, the difference in returning an array versus returning a list does matter in scalar context. Consider this (sad) example:

perl -E 'sub list { return qw/a b c/ } sub array { my @array = qw/a b c/; return @array } say scalar list; say scalar array'

In teaching this, I try to mention that "lists are always used to construct new arrays, hashes and lists". I don't know why, but that helps me to understand that @a = @b turns @b into a list and assigns it to @a. YMMV

So far, lists seem to me coincident with 'array literals'. That is the way I always referred to the (1,2,3) construct, and the name analogous constructs get in other programming languages. (With python, with its tuples, being the odd man out) Am I missing anything?

Arrays *can* be passed to subroutines, in the sense that the original array can be modified by the sub:

perl -wle'sub foo { $_[0] = "ohhai" }; my @arr = (0,1,2); foo(@arr); print "@arr"'

Leave a comment

About Mike Friedman

user-pic Mike Friedman is a professional computer programmer living and working in the New York City area.