我有一个 Perl 脚本,它使用外部工具 (cleartool) 来收集有关文件列表的信息。我想使用 IPC 来避免为每个文件生成一个新进程:
use IPC::Open2;
my ($cin, $cout);
my $child = open2($cout, $cin, 'cleartool');
返回单行的命令效果很好。例如
print $cin "describe -short $file\n";
my $description = <$cout>;
返回多行的命令让我陷入了一个死胡同,不知道如何使用整个响应而不被阻塞读取挂起:
print $cin "lshistory $file\n";
# read and process $cout...
我尝试通过以下方式设置文件句柄以进行非阻塞读取fcntl
:
use Fcntl;
my $flags = '';
fcntl($cout, F_GETFL, $flags);
$flags |= O_NONBLOCK;
fcntl($cout, F_SETFL, $flags);
但 Fcntl 终止并显示消息“您的供应商尚未定义 Fcntl 宏 F_GETFL”。
我尝试使用 IO::Handle 来设置$cout->blocking(0)
但这失败了(它返回undef
和集$!
到“未知错误”)。
我尝试过使用select
在尝试读取之前确定是否有可用数据:
my $rfd = '';
vec($rfd, fileno($cout), 1) = 1;
while (select($rfd, undef, undef, 0) >= 0) {
my $n = read($cout, $buffer, 1024);
print "Read $n bytes\n";
# do something with $buffer...
}
但它没有读任何东西就挂了。有谁知道如何使其工作(在 Windows 上)?