#! perl -slw package threads::Gather; use strict; use Exporter; use threads; use threads::Q; our @ISA = qw[ Exporter threads::Q ]; our @EXPORT = qw[ take gather ]; sub take; sub gather (&$@) { no strict 'refs';# no warnings 'redefine'; my( $code, $Qsize ) = ( shift, shift ); my $Q = threads::Q->new( $Qsize ); my $caller = caller; local *{ "$caller\:\:take" } = sub (_) { $Q->nq( @_ ); }; local $^R = $code; async( sub{ &$code; $Q->nq( undef ); }, @_ )->detach; return sub { $Q->dq; }; } return 1 if caller;