in reply to Closures and sort
--traveleruse strict; $|=1; my @list = qw/ box cow dog apple ant/; sub xcmp{ my $count = 0; sub get_count { return $count } # an inspector as a closure # and another closure sub compare { $count++; return $_[0] cmp $_[1]; } return(\&compare, \&get_count); } my @funcs = xcmp(); print sort { $funcs[0]( $a, $b ) } @list; print "\n"; print $funcs[1](), "\n"; @funcs = xcmp(); print sort { $funcs[0]( $a, $b ) } @list; print "\n"; print $funcs[1](), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Closures and sort
by bobn (Chaplain) on Aug 05, 2003 at 02:36 UTC |