in reply to passing an array ref to subroutine and modifying its elements
sub change { my $arr = shift; $_++ for @$arr; } my $x = 1; my $y = 2; change([ $x, $y ]); print "($x) ($y)\n";
If you want to modify the values themselves, you'll have to do something like:
which is exactly what you have. Or you could say change([ \( $x, $y ) ]);, which is my preference.sub change { my $arr = shift; $$_++ for @$arr; } my $x = 1; my $y = 2; change([ \$x, \$y ]); print "($x) ($y)\n";
|
|---|