The following preserves output order if desired. Here, the chunk_id value is used for ensuring ordered entry into the @found array. It is important for workers to gather even though sending an empty @results. The reason is that the Manager process must know that a given chunk_id has completed.
use strict;
use warnings;
use MCE::Loop;
use MCE::Candy;
my @patterns = ( "biopattern1", "biopattern2", "biopattern3" );
my $search = join('|', @patterns);
my $regex = qr/$search/;
open my $fh, "gunzip -c in.fastq.gz |" or die "open error: $!";
my @found;
MCE::Loop->init(
max_workers => 4,
chunk_size => 50,
RS => "\n@",
gather => MCE::Candy::out_iter_array(\@found)
);
MCE::Loop->run( sub {
my ( $mce, $chunk_ref, $chunk_id ) = @_;
my @results;
for my $i ( 0 .. $#{ $chunk_ref } ) {
if ( $chunk_ref->[$i] =~ /$regex/ ) {
push @results, $chunk_ref->[$i];
}
}
MCE->gather( $chunk_id, @results );
}, $fh );
MCE::Loop->finish();
close $fh;
print join('', @found);