in reply to Re (tilly) 3: $a and strict
in thread $a and strict

Gotcha. So "best practice" would be to qualify the names in a function meant to be (re)used as a sort, and wish Perl generated a warning if a sort was called from a different package. Could do that myself, if a "debug mode" was enabled (a custom "use warning" setting, perhaps). Jumping through hoops to make the function work with the caller's package defeats the point of having $a,$b in the first place. So, create a fully-reusable (member of a class, whatnot) compare function that takes 2 args in the normal way; if someone wants to use it in a sort they call it from their local sort block: sort {Foo::compare($a,$b)} @list;. If you don't write functions meant to be directly used as a sort, the problem doesn't crop up.

Replies are listed 'Best First'.
Re (tilly) 5: $a and strict
by tilly (Archbishop) on Jul 18, 2001 at 21:22 UTC
    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.