package TGrep; use strict; use threads; use Thread::Queue; our $WORKERS = 4; sub g_proxy { my( $code, $Qin, $Qout ) = @_; $Qout->enqueue( $code->() ? $_ : () ) while local $_ = $Qin->dequeue; $Qout->enqueue( undef ); } sub tgrep(&@) { my $workers = $WORKERS; my $code = shift; my( $Qin, $Qout ) = map Thread::Queue->new, 1..2; async( \&g_proxy, $code, $Qin, $Qout )->detach for 1 .. $workers; $Qin->enqueue( map{ ref $_[0] ? @{ $_ } : $_ } @_ ); $Qin->enqueue( (undef) x $workers ); my @results; push @results, $_ while $_ = $Qout->dequeue; return wantarray ? @results : \@results; } sub import { no strict 'refs'; my $pkg = caller; *{ $pkg . '::' . $_ } = *{ $_ } for qw[ tgrep ]; } 1;