use strict; use warnings; use Data::Dump; sub cmp_undef1 { return $a cmp $b if defined $a and defined $b; no warnings 'uninitialized'; return $b cmp $a; # invert order otherwise } sub cmp_undef2 { if (defined $a) { if (defined $b) { $a cmp $b; } else { -1 } } else { if (defined $b) { 1 } else { 0 } } } my @array =( (undef)x 3, qw/c b a/, ("")x3); my @result = sort cmp_undef1 @array; dd \@result; @result = sort cmp_undef2 @array; dd \@result;