Avinash
Avinash

Reputation: 13257

What do I use to read from STDIN/write to STDOUT of a child process?

I have an executable which reads from STDIN and output goes to STDOUT.

I need a Perl script which will fork this executable as a child and write to STDIN of that child process and read from the STDOUT.

This is required for Windows. Any ideas or suggestions?

Upvotes: 2

Views: 886

Answers (2)

Greg Bacon
Greg Bacon

Reputation: 139471

The prescribed solution is the IPC::Open2 module, but the code below may hang on Windows.

#! /usr/bin/perl

use warnings;
use strict;

use IPC::Open2;

my $pid = open2 my $out, my $in, "./myfilter.exe";
die "$0: open2: $!" unless defined $pid;

print $in "$_\n" for qw/ foo bar baz /;
close $in or warn "$0: close: $!";

while (<$out>) {
  chomp;
  print "Parent: [$_]\n";
}

close $out or warn "$0: close: $!";

Upvotes: 3

Related Questions