in reply to limiting scope of 'eval'??

I can't believe nobody's posted this -- try not to use $a and $b as variables, as they're used for sorting in Perl.


($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
=~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re^2: limiting scope of 'eval'??
by BrowserUk (Patriarch) on Aug 23, 2004 at 06:55 UTC

    Maybe because since at least 5.6.1, there isn't any real conflict.

    #! perl -slw use strict; my( $a, $b ) = ( 'mya', 'myb' ); { our( $a, $b ) = ( 'oura', 'ourb' ); my @sorted = sort { $a <=> $b } map{ rand 100 } 0 .. 99; print "$a : $b"; } print "$a : $b"; __END__ P:\test>test oura : ourb mya : myb

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      It's still best to avoid gratuitous use of $a and $b.

      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:

      #! 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