@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...
In reply to Array got modified! by exilepanda
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |