Is it possible to push to an array reference in Perl? Googling has suggested I deference the array first, but this doesn’t really work. It pushes to the deferenced array, not the referenced array.
For example,
my @a = ();
my $a_ref = [@a];
push(@$a_ref,"hello");
print $a[0];
@a will not be updated and this code will fail because the array is still empty
(I’m still learning Perl references, so this might be an incredibly simple question. Sorry if so)
It might help to think in terms of memory addresses instead of variable names.
The string went into a different memory location.
Instead, point the
$a_refvariable to the memory location of list@a.pushaffects memory location 123. Since@aalso refers to memory location 123, its value also changes.