Take the 2-minute tour ×

I have two data structures in Perl:

An array:

my @array2 = ( "1", "2", "3");

for $elem (@array2) {
    print $elem."\n";

Giving me the following output:


And a list:

my @array = [ "1", "2", "3"];                                            

for $elem (@array) {
    print $elem."\n";

Giving the following output:


Obviously, I'd like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array?

share|improve this question
add comment

5 Answers

up vote 42 down vote accepted

Lists in Perl are not data structures, they are positions in the source code, determined by the context around them. Lists are basically the transient structures that Perl uses to move data around. You interact with them with all of Perl's syntax, but you can not work with them as a data type. The data type that is closest to a list is an array.

my @var    =    (1, 2, 3);  # parens needed for precedence, they do not create a list
   ^ an array    ^ a list

say 1, 2, 3;
    ^ a list

say @var;
    ^ a list (of one array, which will expand into 3 values before `say` is called)

When you write [1, 2, 3] what you are doing is creating a scalar array reference. That array reference is initialized with the list 1, 2, 3, and it is the same as creating a named array and taking a reference to it:

[1, 2, 3]   ~~   do {my @x = (1, 2, 3); \@x}

Since the [...] construct creates a scalar, you should hold it in a scalar:

my $array = [1, 2, 3];                                            

for my $elem (@$array) {   # lexical loop variable
    print $elem."\n";

Since you want to operate on the whole array, and not just the reference, you place a @ in front of the $array which dereferences the stored array reference.

share|improve this answer
Thou I think TLP answer is better in terms of describing what this is all about, Your answer has really helped me. Especially the solution at the end. Thanks! –  Marcin Cylke May 17 '11 at 6:10
+1 this should go to man 1 perllists –  malfaux Oct 12 '12 at 14:52
add comment

In Perl, arrays and lists are essentially the same thing, but your second example uses neither. Square brackets surround an array reference (a scalar value), and "assigning" a scalar to an array with something like my @array = $scalar is equivalent to my @array = ($scalar). Thus, the only element of @array in your second example is an array reference.

share|improve this answer
In Perl, arrays and lists are not the same thing, just as scalars and numbers are not the same thing. One is a variable, the other is data that can be stored in that variable. The second example does use a list to specify the contents of the anonymous array that is being created. Square brackets create an array reference. After that, you've got it right :-) –  tadmc May 17 '11 at 3:23
add comment

If you want more clarification, here's a pointer to the relevant docs: http://p3rl.org/REF

You could try and do some things for illustration purposes:

#!perl -lw # -l appends a \n to each print, -w enables warnings
use strict;
my $aryref = [1 .. 5];
print for $aryref;    # prints the stringified reference - not what you want
print for @$aryref;   # dereferencing it lets you access the array
my @ary = $aryref;    # probably not what you want
print for @ary;       # stringified reference again
print for @{$ary[0]}; # dereference first array element (which holds the ref)
share|improve this answer
add comment

The square brackets are used to create an anonymous array. When evaluated, it returns a reference to this array, not the actual array values.

Parentheses have no such hidden property, but simply override precedence inside expressions, much like in math. For example:

my @array = 1,2,3;

Is actually evaluated like this:

my @array = 1;
2,3; # causes the warning "Useless use of constant in void context"

because the = operator has higher precedence than the commas. So to get around that, we use parentheses when assigning arrays, like so:

my @array = (1,2,3);

Your example:

my @array = [1,2,3];

is somewhat like saying this:

my @tmp = (1,2,3);
my @array = \@tmp;

Where the \ is used to create a reference to the @tmp array.

share|improve this answer
add comment

The square brackets create an anonymous array, populate the array with the contents of the brackets, and return a reference to that array. In other words,


is basically the same as

do { my @anon = ("1", "2", "3"); \@anon }

So the code should look like

my $array_ref = [ "1", "2", "3" ];

for (@$array_ref) {  # Short for @{ $array_ref }
    print "$_\n";


my @array_of_refs = ([ "1", "2", "3" ]);

for my $array_ref (@array_of_refs) {
    for (@$array_ref) {
        print "$_\n";
share|improve this answer
add comment

Your Answer


