Push to array reference

It might help to think in terms of memory addresses instead of variable names.

my @a = ();       # Set aside memory address 123 for a list.

my $a_ref = [@a]; # Square brackets set aside memory address 456.
                  # @a COPIES the stuff from address 123 to 456.

push(@$a_ref,"hello"); # Push a string into address 456.

print $a[0]; # Print address 123.

The string went into a different memory location.

Instead, point the $a_ref variable to the memory location of list @a. push affects memory location 123. Since @a also refers to memory location 123, its value also changes.

my $a_ref = \@a;       # Point $a_ref to address 123. 
push(@$a_ref,"hello"); # Push a string into address 123.
print $a[0];           # Print address 123.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)