in reply to Unable to capture STDOUT from system command

... The system() call returns while these programs execute in the background.

If the programs - as it seems - actually do run in the background, the problem is that the initial process (which you start via system or open) will fork another process and terminate itself, thereby also closing the pipe.  And as the command (parent process) has terminated, the respective Perl function will return more or less immediately.

In case you're on Unix you should be able to use a named pipe (aka fifo):

#!/usr/bin/perl my $command = "./program_to_run.pl"; my $fifo = "tmp.fifo"; system "mkfifo $fifo; $command >$fifo 2>&1 &"; open my $fh, "<", $fifo or die "Couldn't open fifo '$fifo': $!"; while (<$fh>) { print ": $_"; } close $fh; unlink $fifo;

The background script (program_to_run.pl):

#!/usr/bin/perl my $pid = fork(); if ($pid) { exit; } else { $| = 1; for (1..3) { print "foo"; print STDERR "bar\n"; sleep 1; } print "done.\n"; }

Output (from the first script):

: foobar : foobar : foobar : done.