If the subroutine's prototype is ($$), the elements to be compared are passed by reference in @_, as for a normal subroutine. This is slower than unprototyped subroutines, where the elements to be compared are passed into the subroutine as the package global variables $a and $b (see example below). Note that in the latter case, it is usually counter-productive to declare $a and $b as lexicals.What a surprise, you did use a prototype. So this will work:
my @names = qw(Brad Aaron Joseph); package FP; sub sortNames ($$) { my ($x, $y) = @_; print "$x vs $y\n"; return $x cmp $y; } package joe; my @sorted = sort FP::sortNames @names;
Note that the sub must already be compiled, or the prototype for the sub must otherwise already be known, when this sort statement is parsed.
Update Much to my surprise, the latter doesn't seem to be true. It works just as well, with the FP::sortNames sub defined after the sort call. Can anyone explain?
In reply to Re^2: sort sub in a different package
by bart
in thread sort sub in a different package
by blahblahblah
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |