#! 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:
#! 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.
Update: Deleted extra code tag |