my %sorts = ( 'case sensitive forward' => sub { \$_[0] cmp \$_[1] }, 'case insensitive forward' => sub { uc \$_[0] cmp uc \$_[1] }, 'case sensitive backward' => sub { \$_[1] cmp \$_[0] }, 'case insensitive backward' => sub { uc \$_[1] cmp uc \$_[0] }, 'case sensitive forward reverse' => sub { reverse(\$_[0]) cmp reverse(\$_[1]) }, 'case insensitive forward reverse' => sub { uc reverse(\$_[0]) cmp uc reverse(\$_[1]) }, 'case sensitive backward reverse' => sub { reverse(\$_[1]) cmp reverse(\$_[0]) }, 'case insensitive backward reverse' => sub { uc reverse(\$_[1]) cmp uc reverse(\$_[0]) }, ); # 5.10+ only #sub do_sorts { # state %cache; # my \$res = \$cache{\$_[0], \$_[1]}; # return if defined(\$res); # foreach my \$sort (@{\$_[2]}) { # \$res ||= \$sorts{\$sort}->(@_) # } # \$cache{\$_[0], \$_[1]} = \$res #} { my %cache; sub do_sorts { my (\$first, \$second, @sort_list) = @_; my \$res = \$cache{\$first, \$second}; return \$res if defined(\$res); foreach my \$sort (@sort_list) { \$res ||= \$sorts{\$sort}->(\$first, \$second); last if \$res } \$cache{\$first, \$second} = \$res } } my @list = qw(foo Bar baz Fuzz); print join(', ', sort { do_sorts(\$a,\$b,('case sensitive forward reverse')) } @list)."\n";