in reply to capturing output of backticks in child process... or something completely different

Here is how to return data from forks in PFM. If you want to put it in a variable that the main script has in scope, use threads and threads:shared.

The following code has been posted here before.

#!/usr/bin/perl use 5.010; use strict; use warnings; use Parallel::ForkManager; #use Data::Printer; my $pm = Parallel::ForkManager->new(2); $pm->run_on_finish( sub { # result from the child will be passed as 6th arg to callback my $res = $_[5]; # p $res; print "$res\n"; } ); for (1..3) { $pm->start and next; # from here and till $pm->finish child process running # do something useful and store result in $res my $res = { aaa => $_ }; # this will terminate child and pass $res to parent process $pm->finish(0, $res); } $pm->wait_all_children;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
  • Comment on Re: capturing output of backticks in child process... or something completely different
  • Download Code

Replies are listed 'Best First'.
Re^2: capturing output of backticks in child process... or something completely different
by abualiga (Scribe) on May 29, 2014 at 22:58 UTC
    many thanks!