exilepanda has asked for the wisdom of the Perl Monks concerning the following question:
@arr = qw/Foo Bar Blaz/; foreach my $ele ( @arr ) { $ele =~ s/^(.)/x$1/; do_something ( $ele ) ; } print "@arr"; # I got 'xFoo xBar xBlaz'
CODE2:
I just found that, in CODE1, the array elements will change after the run. However, I suppose that $ele is a copy from the array, but seems it behaves like a reference. But if I try print ref $ele it returns nothing so its not reference. Anyway, CODE2 is what I would expect.@arr = qw/Foo Bar Blaz/; foreach my $ele ( @arr ) { my $copy = $ele ; $copy =~ s/^(.)/x$1/; do_something ( $copy ) ; } print "@arr"; # I got 'Foo Bar Blaz'
So, I just want to know:
1. Is this normal for the being in CODE1 or I missed something? 2. Is this happen in all Perl version? ( I am using 5.10 now) and was using 5.6~5.8 beforeBecause I am considering to have some revision on modules I wrote before now...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array got modified!
by Util (Priest) on Oct 10, 2011 at 02:24 UTC | |
by exilepanda (Friar) on Oct 10, 2011 at 02:54 UTC | |
by Jenda (Abbot) on Oct 10, 2011 at 10:25 UTC | |
by exilepanda (Friar) on Oct 10, 2011 at 12:51 UTC | |
by Anonymous Monk on Oct 10, 2011 at 13:00 UTC | |
| |
by choroba (Cardinal) on Oct 10, 2011 at 13:44 UTC | |
| |
by BrowserUk (Patriarch) on Oct 10, 2011 at 03:36 UTC | |
by exilepanda (Friar) on Oct 10, 2011 at 03:54 UTC | |
by choroba (Cardinal) on Oct 10, 2011 at 07:31 UTC |