my $sorter = ( $reverse ? sub { $b cmp $a } : sub { $a cmp $b } ); for (sort $sorter @array) { ... }