This may be useful:
#!/usr/bin/perl open (my $child, "-|","./child.pl") or die("$!"); while (<$child>) { print "P: $_"; } close($child);
public function, from perldoc :
For three or more arguments, if MODE is | -, the file name is interpreted as the command for which the output should be sent, and if MODE is - | , the file name is interpreted as a command that outputs the output to us.
If you don't want to touch stdout, you need cooperation with a child, and then you can use named pipes:
parent.pl
#!/usr/bin/perl use strict; use warnings; use Fcntl; use POSIX; my $fpath = '.named.pipe'; mkfifo($fpath, 0666) or die "mknod $!"; system("perl child.pl &"); sysopen(my $fifo, $fpath, O_RDONLY) or die "sysopen: $!"; while (<$fifo>) { print "P: $_"; } close($fifo); unlink($fifo);
child.pl
#!/usr/bin/perl use strict; use warnings; use Fcntl; use POSIX; my $fpath = '.named.pipe'; sysopen(my $fifo, $fpath, O_WRONLY) or die "sysopen: $!"; print "screen hello\n"; print $fifo "parent hello\n"; close($fifo);
source share