abhishes has asked for the wisdom of the Perl Monks concerning the following question:
I wrote the following program and the output really confused me.
use warnings; use strict; my @a = ("a", "b", "c"); Foo(\@a); print "$_ " foreach(@a); sub Foo { my $refA = shift; my @b = @$refA; $b[0] = "hello"; $b[1] = "world"; $b[2] = "Perl"; }
The output of the code was "a b c ".
How?? Didn't I pass a reference? If so, when I alter items against the reference, the change should be visible outside the subroutine as well!
I searched perl monks site found this node
but its still not clear to me... why were my changes lost. regards,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Changing items of an array in a function
by Anonymous Monk on May 14, 2003 at 13:46 UTC | |
|
Re: Changing items of an array in a function
by broquaint (Abbot) on May 14, 2003 at 13:51 UTC | |
|
Re: Changing items of an array in a function
by edan (Curate) on May 14, 2003 at 13:52 UTC | |
|
Re: Changing items of an array in a function
by physi (Friar) on May 14, 2003 at 15:45 UTC |