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

Only within the scope of the my, or anywhere after?

What about having the sort function qualify the names, eg. $main::a, to avoid that issue?

Replies are listed 'Best First'.
Re (tilly) 3: $a and strict
by tilly (Archbishop) on Jul 18, 2001 at 19:15 UTC
    There is only an issue with sort if you are not using the fully qualified package name and you are within the scope of a my declaration. (Or an our declaration from a different package.)

    Of course your co-workers may still want a clearer variable name...

      Of course, one can skirt the issue in version 5.6+ using prototypes because then the arguments arrive in @_ as with ordinary subroutines (though it will be slower than the package variable method):

      #!/usr/bin/perl -w use strict; package Foo; sub numeric1 ($$) { my($a, $b) = @_; # could use any variable names $a <=> $b } # or just sub numeric2 ($$) {$_[0] <=> $_[1]} package main; my($a, $b) = (13, 42); my @array = (3, 2, 5, 3, 4, 1); my @sorted1 = sort Foo::numeric1 @array; print "@sorted1\n"; my @sorted2 = sort Foo::numeric2 @array; print "@sorted2\n";
        tiny-e says "Whoa, look at that node, it's huge!" ;)
        sub sorte {shift<=>shift}
      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.

        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.