I've got some code that doesn't work the way I expect, and I'd appreciate some help figuring out how to get it working the way I DO expect.
I'd like to use a subroutine to modify an input array. I figured that if I passed in a reference to the array, any changes I made to it would be reflected in the caller's version, too. But it apparently doesn't work that way.
my @test_array = qw (zero one two three);
shift_array(\@test_array);
print "POST SUBROUTINE: $test_array[0]\n";
sub shift_array {
my @array = @{(shift)};
shift @array;
print "AFTER SHIFT IN SUB: $array[0]\n";
}
This prints:
AFTER SHIFT IN SUB: one POST SUBROUTINE: zero
I expected it to print one both times.
So my question is two-fold:
1) Why isn't it behaving the way I think it should? Does passing a reference to an array create a copy of the array?
2) How do I get the behavior I WAS expecting? How do I I get a subroutine to slide one or more elements off of the front of caller's copy of an input array?
Thanks in advance for any insight you can offer.