my $sub = 'sub { my ($a, $b) = @_; '; if ($type == NUMERIC) { $sub .= ' $a <=> $b '; } else ... $sub .= '}'; my $sortSub = eval $sub;