Need help getting perl array out of scalar context

78 Views Asked by At

I have a perl array I need to store in the following way:

 $self->{spec}->{allImages} = @allImages;

Then I need to retrieve the contents later:

 print Dumper($self->{spec}->{allImages});

This yields:

 $VAR1 = 10;

(the number of items in the array).

How can I break out of scalar context and get $self->{spec}->{allImages} back as a list?

2

There are 2 best solutions below

1
Andy Lester On BEST ANSWER

Each hash value can only be a scalar.

You must store a reference to the array:

$self->{spec}->{allImages} = \@allImages;

http://perldoc.perl.org/perlreftut.html will give you more tutorial.

0
Jonathan Leffler On

You need to change the assignment:

$self->{spec}->{allImages} = \@allImages;

This creates an array-ref that you can use.