Perl's magic sort variables: $a and $b


When I call my variables $a or $b they don't do what I expect. What's up?


Here's an often-used, but little-remembered fact about Perl that has caused some confusion on more than one occasion: $a and $b are special.

They are (among other things) used in custom sort blocks, but unfortunately the Perl documentation uses them in many examples. I say that it is unfortunate because the use strict pragma seems to ignore these two variables. If you do the following:

use strict;

$a = "foo";
$b = "bar";
$c = "baz";
$d = "quux";

You will see that only $c and $d are flagged as errors.

My advice is to avoid using $a and $b except for custom sort blocks and other places that require their use.