Perl Background Process - Finshed Yet?

I have a perl process I want to run in background in a cgi, but do not
want to continue until process finished code looks like this.

sub calc(){
do calculations
return value
}

In another Perl program I call above function such as

&calc();

I want to continue after process finishes.

Any Ideas on how to do this.

Thanks

for finer control over processes, check out Proc::Simple
Please note that sub calc () is a subroutine, and not a process.

If you are calling this subroutine from some other place, then the regular behavior is what you have described:

#!/usr/bin/perl
# sub_trial.pl
use strict;
sub calc ()
{
    print "in calc() now ... \n";
    sleep 10;  # wait for 10 seconds
      # or do something here
}
print "we're in main script ... \n";
calc ();
print "and continued ... \n";

actually it is a little more complicated then my simple example, but
basically by the time the background process &calc finishes my page
has loaded, so I have to keep refreshing the page until &calc() finishes.

The reason for this is, once &calc enters values
into database, then final page can be reloaded only once instead of
multiple times.

Obviously, I will have to run it in a

unless(my $pid = fork()){&calc();}

or
something like that. When &calc() finishes reload page once.

Does this help (I mean the non-blocking wait example shown)?

waitpid - perldoc.perl.org

Of course, as a CGI script is executed once for each HTTP request, you must keep the process ID you are monitoring somewhere, say on the filesystem or session.

Its funny none of the perl books I have had anything on waitpid. I had to go to the Stevens book, but waitpid is the solution I came up with too.
.

defined(my $pid = fork()) or die "Cannot fork: $!";
if($pid){
    ## waitpid($pid, 0);
    ## or if you want the pid
    do{
        $kid = waitpid(-1, WNOHANG);
        $count_pid = $kid;
    }until $kid > 0;

    $out .= qq|<hidden type="count_pid" value="$count_pid">|;
    ## javascript redirect here
}else{
    &calc();
    CORE::exit(0);
}

It works good, but still looking for a way to improve. I may have to use AJAX or something to get rid of reloads.