http://qs321.pair.com?node_id=1230210


in reply to How can I read DATA in parent and child?

Hi Skeeve,

Update: Ah, the OP wants to read DATA independently. See tybalt89's solution.

Update: Currently, the following demo works on Unix platforms with IO::FDPass: automatically loaded by MCE::Shared if present in Perl. The next update to MCE::Shared on CPAN will support reading from the DATA handle without involving IO::FDPass and work on Windows including Cygwin.

MCE::Shared is not exclusive to only MCE parallel modules: e.g. MCE, MCE::Flow, MCE::Hobo, and etcetera. MCE::Shared also works with threads, even fork shown below, and other parallel modules found on metacpan.

Code

use strict; use warnings; use MCE::Shared; mce_open my $shared_fh, '<', \*DATA; my $pid= fork(); die unless defined $pid; if ($pid == 0) { print "Start Child\n"; while (<$shared_fh>) { print "C: $_"; sleep 1; } print "Stop Child\n"; } else { print "Start Parent\n"; while (<$shared_fh>) { print "P: $_"; sleep 1; } print "Stop Parent\n"; } __DATA__ a b c d e f g h i j

Output

Start Parent Start Child P: a C: b C: c P: d C: e P: f C: g P: h C: i P: j Stop Child Stop Parent

Regards, Mario