in reply to Re^2: limiting scope of 'eval'??
in thread limiting scope of 'eval'??
When $a and $b are declared properly, no conflict. But if you forget to declare them, you can quietly get action at a distance.
#! perl -slw use strict; ( $a, $b ) = ( 'mya', 'myb' ); # imagine several lines of intervening code { our( $a, $b ) = ( 'oura', 'ourb' ); my @sorted = sort { $a <=> $b } map{ rand 100 } 0 .. 99; print "$a : $b"; } print "$a : $b"; __END__ C:\s>perl sortab.pl oura : ourb oura : ourb #no warnings, $a and $b are quietly changed
The script dies if they are declared (with my instead of our) in the same scope as the sort routine:
Update: Deleted extra code tag#! perl -slw use strict; my( $a, $b ) = ( 'mya', 'myb' ); { my( $a, $b ) = ( 'oura', 'ourb' ); my @sorted = sort { $a <=> $b } map{ rand 100 } 0 .. 99; print "$a : $b"; } print "$a : $b"; __END__ C:\S\pl>perl sortab.pl Can't use "my $a" in sort comparison at sortab.pl line 10.
|
|---|