in reply to Is this code correct

Basically that code looks ok.

As it uses an if/else the ternary operator came to my mind to write it differently:

my $sixth = 'whatever'; $var = defined($var) ? $sixth : 3;

As I personally avoid to use variables $a and $b in normal code (only in sort), I used $var here.