in reply to foreach my $var scope oddness
Output :-use strict; use warnings; my @outer = (qw| one two three |); print "First\n"; foreach my $o (@outer) { print $o, "\n"; } print "Second\n"; foreach my $o (@outer) { $o .= 'HIII'; print $o, "\n"; } print "ARRAY " , @outer , "\n"; #print $o, "\n"; # Comment out compile time error print "Third\n"; foreach my $o (@outer) { print $o, "\n"; }
This is just to make you clear on what had happened in the following loop:-First one two three Second oneHIII twoHIII threeHIII ARRAY oneHIIItwoHIIIthreeHIII Third oneHIII twoHIII threeHIII
The above loop actually modifies the value in the Memory region where it is pointing to. $o points the memory region of all indexes of array. so It modifies the element of array.foreach my $o (@outer) { $o .= 'HIII'; print $o, "\n"; }
|
|---|