Turning to what Hobbs provided, if you want to return data from a child process to a parent process, you need to have an external channel. I ended up using tempfs because it was as simple as a file, but it did not put IO images on disk.
** Important ** You need to exit the child process, because otherwise the "child" process will continue along with the same script, and you will get two print statements. Thus, in the example below, foreach (@stdoutput) will happen twice, even though it is only once in the script.
$shm_id = time; #get unique name for file - example "1452463743" $shm_file = "/dev/shm/$shm_id.tmp"; #set filename in tempfs $| = 1; #suffering from buffering print ("Activity Indicator: "); #No new line here defined(my $pid = fork) or die "Couldn't fork: $!"; if (!$pid) { # Child @stdoutput=`/usr/home/script.pl -o $parameter`; #get output of external command open (SHM, ">$shm_file"); foreach (@stdoutput) { print SHM ("$_"); #populate file in tempfs } close (SHM); exit; #quit the child process (will not kill parent script) } else { # Parent while (! waitpid($pid, WNOHANG)) { print ("\#"); # prints a progress bar sleep 5; } } print ("\n"); #finish up bar and go to new line open (SHM, "$shm_file"); @stdoutput = <SHM>; #Now open the file and read it. Now array is in parent close (SHM); unlink ($shm_file); #deletes the tempfs file chomp(@stdoutput); foreach (@stdoutput) { print ("$_\n"); #print results of external script }
Westrock
source share