So, I have the following code that works:
my $cmp;
if ( $action eq DEL ) {
$cmp = \&cmpb;
}
else {
$cmp = \&cmpf;
}
foreach my $x ( sort $cmp keys %y ) {
}
Both cmpb and cmpf are here:
sub cmpf { $a cmp $b }
sub cmpb { $b cmp $a }
Now my question is: I would prefer something like:
foreach my $x ( sort $action eq DEL ? \&cmpb : \&cmpf keys %y ) {
}
Or even better:
foreach my $x ( sort $action eq DEL ? { $a cmp $b } : { $b cmp $a } keys %y ) {
}
So, two questions. Firstly, what is the correct way to include these functions in a series, and secondly, why not work higher?
source
share