tc1364 has asked for the wisdom of the Perl Monks concerning the following question:

How do you use wait so that the sub routine completes (returns 0 or 1) and is checked by the "if" statement before allowing the foreach command to advance?
foreach $cmd (@commands) { $ret = tlc($cmd); if ($ret >= 1) { } }

Replies are listed 'Best First'.
Re: Wait Question
by Joost (Canon) on Dec 12, 2006 at 19:54 UTC
Re: Wait Question
by madbombX (Hermit) on Dec 12, 2006 at 20:16 UTC
    Assuming that you want to wait on a subroutine to finish that has system commands (or even just parts of the script that you may like to run in parallel), take a look at Parallel::ForkManager. Put whatever wait code you want in the run_on_wait() sub. Everything else goes into the foreach loop.
    use Parallel::ForkManager; # Begin ForkManager my $_max_procs = 5; my $_pm = new Parallel::ForkManager($_max_procs); # Log at process fork $_pm ->run_on_start( sub { my ($pid, $host) = @_; print "Forking process PID: $pid\n"; } ); # Log at process copmletion $_pm ->run_on_finish( sub { my ($pid, $exit_code, $func) = @_; print "Finishing up process PID: $pid\n"; } ); # This run_on_wait is currently set to print every 5 sec # It can easily be modified to write to a socket every X sec $_pm->run_on_wait( sub { print "Waiting for children to finish.\n" }, 5.0 ); foreach my $cmd (@commands) { # Fork off the children my $pid = $_pm->start($cmd) and next; # run subroutine to wait on here # Closing the forked process $_pm->finish; } # Ensure all children have finished $_pm->wait_all_children; exit(1);
Re: Wait Question
by andyford (Curate) on Dec 12, 2006 at 20:01 UTC

    Can you give us more details? Are you executing system commands that go into the background perhaps?
    What does "tlc" do?

    It's best to post a complete, but minimal, working example.

    non-Perl: Andy Ford