Well the best practice is to not expect sort functions to be passable between packages.
But if you want to, here is an example of both how to write something that can return a sort function to another package (requires symbolic refs) and how to sort using a sort function from another package (requires eval):
use strict;
print join "\n", Foo::sort_your_pack(Bar::ret_num_asc(), 1..10, -5..-3
+);
package Foo;
use Carp;
# Takes a package. Will return a closure that generates sorts in that
+ package.
{
my %sort_gen;
sub ret_sort_gen {
my $pack = shift;
my $set_evalname = "\n#line 1 \"'sorter for $pack'\"";
return $sort_gen{$pack} ||= eval qq(
$set_evalname
package $pack;
sub {
my \$sort_fn = shift;
unless (UNIVERSAL::isa(\$sort_fn, 'CODE')) {
Carp::confess("Not a sort function");
}
return sub {sort \$sort_fn \@_;}
}
) || die $@;
}
}
# Takes a sort function and a package. Returns a closure that sorts u
+sing
# that sort function in that package.
sub sorter {
my $sort_fn = shift;
my $sort_gen = ret_sort_gen(shift || caller);
return $sort_gen->($sort_fn);
}
# Example usage of the sorter subroutine.
sub sort_your_pack {
my $sorter = sorter(shift(@_), caller);
return $sorter->(@_);
}
package Bar;
# Takes an optional package. Returns a basic comparison function
sub ret_num_asc {
my $pack = shift || caller;
no strict 'refs';
return sub {${"$pack\::a"} <=> ${"$pack\::b"}};
}
__END__
The ease of setting up interesting sorts is not one of Perl's strengths. |