use strict;
use warnings;
use MCE::Loop;
use MCE::Shared;
# Configure MCE to run with 4 workers.
# A worker receives 1 item per iteration.
MCE::Loop->init(
max_workers => 4,
chunk_size => 1,
);
# Populate an array with test data.
my @data_files = qw/ file1 file2 file3 file4 /;
# Open a shared file handle to the external cat command.
mce_open my $fh, "| cat" or die "open error: $!\n";
# Process the array in parallel.
# Workers send data to the shared file handle.
mce_loop {
my $file = $_;
printf $fh "wid: %d, name: %s\n", MCE->wid(), $file;
} @data_files;
# Close the shared file handle.
close $fh;
####
wid: 3, name: file1
wid: 2, name: file2
wid: 4, name: file3
wid: 1, name: file4
####
mce_open my $fh, '| c:/cygwin/bin/cat.exe' or die "open error: $!\n";