use 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";