in reply to Re^4: Small troubles with references
in thread Small troubles with references
Consider
sub foo { my $x=shift; $x="Changed1"; $_[0]="Changed2"; } my $i="Orig1"; my $j="Orig2"; print "$i,$j\n"; foo($i,$j); print "$i,$j\n"; __END__ Orig1,Orig2 Orig1,Changed2
Assigning to $_[$x] is the same as assigning to the original value that was passed into the subroutine. This is because its an alias. Its not a copy. IOW, its where two or more variable names refer to the same memory location. Its much as if you did
sub bar { my $ref=shift; $$ref='changed' } bar(\$j);
Except you dont have to deref before you do the assignment.
for() aliases, as does map, and a few other places where it can be useful to have this behaviour.
|
---|