in reply to Parallel::ForkManager problem

if all you need is passing a boolean ok value from the child to the parent, in Parallel::ForkManager you can use the run_on_finish callback to get the exit code of the child.

As I am the author of Proc::Queue let me use that module instead...

use Proc::Queue size => 10; sub whatEver { my $list = shift; my (%pid, %result); foreach my $query (keys %$list) { my $pid = fork; defined $pid or die "fork failed"; if (!$pid) { my $ok = do_whatever(); exit (!$ok); } else { $pid{$query} = $pid; } } for my $query (keys %pid) { if (waitpid($pid{$query}, 0) > 0) { $result{$query} = ! $? } } print Dumper("2", \%result); }

Replies are listed 'Best First'.
Re^2: Parallel::ForkManager problem
by Murcia (Monk) on Mar 28, 2006 at 15:39 UTC
    $result{$query} = ! $? can you explain this! Please
      Murcia,
      If you look in perlvar, you will see that $? is the status returned by the last closed pipe. The ! is a way to booleanize it. If it has a non-zero value, it becomes 0 and if it had a 0 value, it becomes 1.

      Cheers - L~R

        If you look in perlvar, you will see that $? is the status returned by the last closed pipe.

        well, no, you have stoped reading too soon:

        $? The status returned by the last pipe close, backtick (``) command, successful call to wait() or waitpid(), or from the system() operator.
        in my code, it is set to the exit code of the child by waitpid.

        It is negated because by convention, exit code 0 means success and any other value failure. Also because this way I don't have to split $? into the signal part and the exit code:

        $signal = $? & 255; $exit_code = $? >> 8;
        But the problem of scope remains with your module, or?