我用什么来读取STDIN /写入子进程的STDOUT?

时间:2010-08-16 13:13:30

标签: windows perl

我有一个可执行文件,它从STDIN读取并输出到STDOUT。

我需要一个Perl脚本,它将这个可执行文件作为子进行分叉,并写入该子进程的STDIN并从STDOUT读取。

这是Windows所必需的。有什么想法或建议吗?

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

规定的解决方案是IPC::Open2模块,但下面的代码可能会在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: $!";