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.
It only becomes an issue if you declare them as lexical variables.$ perl -le '$a="foo"; my @n = (3,2,1); print for sort {$a<=>$b} @n; pr +int $a' 1 2 3 foo
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' Can't use "my $a" in sort comparison at -e line 1.
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.$ perl -le 'my $a; my @n = (3,2,1); print for sort {$::a<=>$::b} @n;' 1 2 3
-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 |