0

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?

iLikeDirt
  • 606
  • 5
  • 17
  • 1
    possible duplicate of [Array in value of hash perl](http://stackoverflow.com/questions/13965196/array-in-value-of-hash-perl) – Jonathan Hall Feb 12 '15 at 20:16

2 Answers2

9

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.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 2
    Or a copy, `[ @allImages ]`, which is sometimes what you want instead. The reference is a stand in for the array so if you change it through a ref, the real array and therefore all its referents change as well. – Ashley Feb 12 '15 at 22:34
1

You need to change the assignment:

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

This creates an array-ref that you can use.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278