Unless the OP edited the code, his example does not have a closure. He alluded to tracking the exit codes via a HASH but that sounded more like a global than a closure. I believe the OP may be looking for someway to update his database inside the run_on_finish command -- in that case he could just close over the database handle instead of some data structure which could be manipulated *after* the run of all the children.
Update: ... forgot an (untested) example:
my $dbh = create_dbh_handle();
my $code = create_closure( $dbh );
....
my $pm = Parallel::ForkManager->new( $MAX_NR_PROCESSES );
$pm->run_on_finish( $code );
foreach ... {
my $pid = $pm->start and next;
...
$pm->finish; # Terminates the child process
}
sub create_closure {
my( $dbh ) = @_;
return sub {
my ($pid, $exit_code, $ident) = @_;
my $sql = "insert into table values( $pid, $exit_code )";
$dbh->do( $sql );
}
}
|