in reply to How to get sort-like semantics?

Is the problem you are having in placing this function into a module? If that is the case, then the problem is getting ahold of the caller's $a and $b. Check out the code from reduce in List::Util.

sub reduce (&@) { my $code = shift; no strict 'refs'; return shift unless @_ > 1; use vars qw($a $b); my $caller = caller; local(*{$caller."::a"}) = \my $a; local(*{$caller."::b"}) = \my $b; $a = shift; foreach (@_) { $b = $_; $a = &{$code}(); } $a; }

Update: D'oh, a day late and a dollar short.

Good Day,
    Dean

Replies are listed 'Best First'.
Re^2: How to get sort-like semantics?
by friedo (Prior) on Oct 25, 2007 at 13:59 UTC
    The use vars isn't necessary, since you're using a lexical $a and $b.

      I was wondering about that when I copied the code over. My conjecture was that use vars was needed for an older version of perl, but I have no guess as to why an older perl might need it or what version of perl needs it.

      Good Day,
          Dean