develooper Front page | perl.perl5.porters | Postings from April 2000

Can't use "my $a" in sort comparison - why only comparison?

Peter Scott
April 27, 2000 13:13
Can't use "my $a" in sort comparison - why only comparison?
Message ID:
The diagnostic for this error states:

     (F) The global variables $a and $b are reserved for sort comparisons.
     You mentioned $a or $b in the same line as the <=> or cmp operator,
     and the variable had earlier been declared as a lexical variable.
     Either qualify the sort variable with the package name, or rename the
     lexical variable.

And indeed, it only complains under those circumstances.  If I am foolish 
enough to do

my $a;
my @sorted_list = sort { sortfunc($a, $b) } @list;

then it does not complain (and the list is unlikely to get sorted - could 
be a good obfuscated entry in the making here).

Why can't the error be issued if a lexical $a/$b shows up anywhere in a 
sort block, not just in the same line as a comparison?

Peter Scott
Pacific Systems Design Technologies Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About