#!/usr/bin/perl -w
use strict;
my $aref = [1, 2, 3];
my @a = @$aref; # this line
$a[1] = 99;
print "aref = @$aref\n";
print "a = @a\n";
produces output:
aref = 1 2 3
a = 1 99 3
The output shows that @a and @$aref do not refer to the same array.
The marked line is where my problem lies. The value of scalar $aref is a reference to an anonymous array. In the marked line I was hoping to be able to make the array variable @a refer to that array, but what happens is that the anonymous array is copied and @a refers to a copy of the anonymous array. The assignment and print statements show this.
I understand that when you assign to an array the right hand side of the assignment is a list context, so the @$aref is coerced to a list of its elements. Is there a way to give the name @a to the array referred to by $aref?
@$aref?@aa reference,my $a = $aref$aref->[1]etc.. The old-fashioned way is${$aref}[1](bad) or$$aref[1](worse). Also note thatuse warningsis superior in every way to-won the command line or shebang line