What’s the difference between a list and an array?

I recently updated perlfaq4‘s answer to “What’s the difference between a list and an array?”. The difference between data and variables is often lost of the person who starts their programming career in a high level language.

We hit this subject pretty hard in the first chapter of Effective Perl Programming, 2nd Edition in at least three Items:

  • Item 9: Know the difference between lists and arrays.
  • Item 10: Don’t assign undef when you want an empty array.
  • Item 12: Understand context and how it affects operations.

Here’s the current answer in perlfaq4:


A list is a fixed collection of scalars. An array is a variable that holds a variable collection of scalars. An array can supply its collection for list operations, so list operations also work on arrays:

# slices
( 'dog', 'cat', 'bird' )[2,3];
@animals[2,3];

# iteration
foreach ( qw( dog cat bird ) ) { ... }
foreach ( @animals ) { ... }

my @three = grep { length == 3 } qw( dog cat bird );
my @three = grep { length == 3 } @animals;

# supply an argument list
wash_animals( qw( dog cat bird ) );
wash_animals( @animals );

Array operations, which change the scalars, reaaranges them, or adds or subtracts some scalars, only work on arrays. These can’t work on a list, which is fixed. Array operations include shift, unshift, push, pop, and splice.

An array can also change its length:

$#animals = 1;  # truncate to two elements
$#animals = 10000; # pre-extend to 10,001 elements

You can change an array element, but you can’t change a list element:

$animals[0] = 'Rottweiler';
qw( dog cat bird )[0] = 'Rottweiler'; # syntax error!

foreach ( @animals ) {
	s/^d/fr/;  # works fine
	}

foreach ( qw( dog cat bird ) ) {
	s/^d/fr/;  # Error! Modification of read only value!
	}

However, if the list element is itself a variable, it appears that you can change a list element. However, the list element is the variable, not the data. You’re not changing the list element, but something the list element refers to. The list element itself doesn’t change: it’s still the same variable.

You also have to be careful about context. You can assign an array to a scalar to get the number of elements in the array. This only works for arrays, though:

my $count = @animals;  # only works with arrays

If you try to do the same thing with what you think is a list, you get a quite different result. Although it looks like you have a list on the righthand side, Perl actually sees a bunch of scalars separated by a comma:

my $scalar = ( 'dog', 'cat', 'bird' );  # $scalar gets bird

Since you’re assigning to a scalar, the righthand side is in scalar context. The comma operator (yes, it’s an operator!) in scalar context evaluates its lefthand side, throws away the result, and evaluates it’s righthand side and returns the result. In effect, that list-lookalike assigns to $scalar it’s rightmost value. Many people mess this up becuase they choose a list-lookalike whose last element is also the count they expect:

my $scalar = ( 1, 2, 3 );  # $scalar gets 3, accidentally

Get a review copy of Effective Perl Programming

Addison-Wesley is ramping up its promotion of Effective Perl Programming, 2nd Edition. They’ll send out several free copies to people who like to review books and tell their friends about it. If you’re interested in getting one of those review copies, send me a note at . If I don’t already know you, introduce yourself and send me some links to your previous book reviews, your blog, and so on.

Effective Perl Programming at Frozen Perl 2010

On Friday, February 5, 2010 at Frozen Perl, I’m teaching a new master class based on our book, Effective Perl Programming, 2nd Edition. The master class is a low-cost, condensed class format given at a public Perl event. The cost of this one-day class is $100, and there are a limited number of $25 seats for full-time students.

In the one-day class for intermediate Perl programmers, I’ll cover selected topics from the book, including:

  • Working with Unicode in Perl
  • Tricks with filehandles
  • New regex features in Perl 5.10 and later
  • Playing with pack
  • Using closures to make things simpler
  • and other topics as time allows

Although the book hasn’t been published yet, it is available for pre-order on Amazon, and attendees to the class can get a sneak peek at the working manuscript as well as a soft copy of the course slides.

Effective Perl Programming, the blog

Effective Perl Programming, 2nd Edition, front cover. Josh McAdams and I recently gave Addison Wesley our final manuscript for Effective Perl Programming, 2nd Edition, an update to Joseph Hall’s excellent first edition published in 1998. Not only have we updated the existing material up to Perl 5.10, but we’ve also quite a bit of new material for the evolution of Perl and its practices in the last 12 years.

We’re not stopping there, though. We had plenty more that we wanted to write and couldn’t fit in the book. Sometimes we had more to say on an item in the book and sometimes we wanted to include a topic that didn’t have the space for it. We will trickle out that extra material here. Additionally, we’d like to keep up a conversation with the readers of the latest edition to refine and expand the material that is already there.

Perl 5.8 features

Perl 5.8, mostly a release that improved much of the unseen things in Perl, did have some notable, user-visible features.


  • Restricted hashes with Hash::Util (5.8.0)
  • Use sitecustomize.pl to affect all programs (5.8.7)
  • PerlIO is now the default (5.8.0)