http://www.perlmonks.org?node_id=41695


in reply to Dynamic sort algorithm selection

This does actually work the way you expected, set up some subs with known names, then give the sort routine a variable containing that name:

sub ascii {$a cmp $b} sub alpha {lc($a) cmp lc($b)} @a = qw(a A b B D d c C); $subname = 'ascii'; @sorted = sort $subname @a; print @sorted; $subname = 'alpha'; @sorted = sort $subname @a; print @sorted;

If you are having trouble with an earlier version of perl, then first try upgrading, failing that, you can always use eval:

 eval '@sorted = sort ' . $subname . ' @unsorted';

But that's icky, so try not to do that.