I know I can do the following:
open(STDOUT, "|-", "cmd");
Which presumably results in the following:
fork();exec("cmd");- Link STDOUT of original process to STDIN of new process.
What I’d like to do is step 2 above, i.e. don’t do the exec(). Just have two copies of the current process continuing, the STDIN of the child linked to the STDOUT of the parent.
Which would then allow me to continue on like this:
if (! $pid)
{
# Handle data in child here
open OUTFILE1 $file1;
open OUTFILE2 $file2;
my $i = 0;
while (<>)
{
print (i % 2 ? OUTFILE1 : OUTFILE2) "Line $i: $_";
++$i;
}
exit;
}
else
{
# Continue parent here
print STDOUT "Hello World from print()";
system("echo Hello World from system()");
}
The above code should add a line number to the output and write this data alternatively line by line to different files (I know it’s a bit silly, but it’s just an example).
Is there a way or a library that does this? I considered perltie but it didn’t seem to handle results from system(), hence I believe I need a separate process to actually pipe STDOUT to.
IIRC, you simply want
but
open3also only takes one line, has better error reporting, and would allow you to capture STDERR as well.