use 5.012_002;
use strict;
use warnings;
my %hash = (
this => 1,
that => 4,
the => undef,
other => undef,
those => 2,
it => 0,
);
# First simply disable the warning temporarily.
{
no warnings qw/uninitialized/;
my @warnsorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
say "Without Warnings:\t@warnsorted";
}
# Second, check each item for definedness.
my @defsorted = sort {
( defined( $hash{$a} ) && $hash{$a} ) <=>
( defined( $hash{$b} ) && $hash{$b} )
} keys %hash;
say "Testing Definedness:\t@defsorted";
# Third, sort with definedness as a criteria
my @critsorted = sort {
defined( $hash{$a} ) <=> defined( $hash{$b} ) or
( defined( $hash{$a} ) && $hash{$a} ) <=>
( defined( $hash{$b} ) && $hash{$b} )
} keys %hash;
say "Defined as Criteria:\t@critsorted";
####
Without Warnings: the it other this those that
Testing Definedness: the it other this those that
Defined as Criteria: the other it this those that
####
{
no warnings qw/uninitialized/;
my @sorted = sort {
defined( $hash{$a} ) <=> defined( $hash{$b} ) or
$hash{$a} <=> $hash{$b}
} keys %hash;
say "Defined as Criteria:\t@sorted";
}