Treat undef in Perl's  operator as +inf instead of 0
In fact, Perl treats undef as 0 in numeric contexts, and warns about that (why there's no -w in your one-liner?). You can use the defined-or operator to handle it:

sort { ($a // 'Inf') &lt;=&gt; ($b // 'Inf') } 3, undef, 2, 1


You need Perl v5.10+ for it to work, in older versions, you have to be more verbose:

(defined $a ? $a : 'Inf')