in reply to Re^2: Split a string into items of constant length
in thread Split a string into items of constant length

$a should not be used as a general purpose variable

It's probably better to explain that rather than refer to a manpage that won't. At least, not very well. Yes, sort uses package variable $a and $b. It also localizes them.

$ perl -le '$a="foo"; my @n = (3,2,1); print for sort {$a<=>$b} @n; pr +int $a' 1 2 3 foo
It only becomes an issue if you declare them as lexical variables.
$ perl -le 'my $a; my @n = (3,2,1); print for sort {$a<=>$b} @n' Can't use "my $a" in sort comparison at -e line 1.
And the better fix is probably not to avoid $a and $b but to be explicit in your sort blocks and subs by using $::a and $::b (or $Foo::a and $Foo::b if you are in package Foo) explicitly.
$ perl -le 'my $a; my @n = (3,2,1); print for sort {$::a<=>$::b} @n;' 1 2 3
That isn't to say avoiding $a and $b is a bad thing... they are generally lousy variable names anyway. But I often use them in one-liners. So long as you know when and, more importantly, why it can be an issue, there's no harm in it.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^4: Split a string into items of constant length
by blazar (Canon) on Oct 04, 2005 at 11:56 UTC
    That isn't to say avoiding $a and $b is a bad thing... they are generally lousy variable names anyway. But I often use them in one-liners.
    Me too. Also in golf/obfu when I want to keep the code strict safe (even if there would be other built in vars available and provided that it's easy to do so), like in this one.