【发布时间】:2010-10-06 19:51:48
【问题描述】:
我正在尝试将 Perl 脚本从 Unix 移植到 Windows,但由于 open 函数中不支持分叉管道,我几乎不可能有时间让它工作。代码如下:
sub p4_get_file_content {
my $filespec = shift;
return 'Content placeholder!' if ($options{'dry-run'});
debug("p4_get_file_content: $filespec\n");
local *P4_OUTPUT;
local $/ = undef;
my $pid = open(P4_OUTPUT, "-|");
die "Fork failed: $!" unless defined $pid;
if ($pid == 0) { # child
my $p4 = p4_init();
my $result = undef;
$result = $p4->Run('print', $filespec);
die $p4->Errors() if $p4->ErrorCount();
if (ref $result eq 'ARRAY') {
for (my $i = 1; $i < @$result; $i++) {
print $result->[$i];
}
}
$p4->Disconnect();
exit 0;
}
my $content = <P4_OUTPUT>;
close(P4_OUTPUT) or die "Close failed: ($?) $!";
return $content;
}
错误是:
'-' is not recognized as an internal or external command,
operable program or batch file.
有谁知道如何进行这项工作?谢谢!
迈克
【问题讨论】:
-
如果父母只是坐等孩子的输出,为什么要分叉?
-
for (my $i = 1; $i [$i]; } 最好写成 shift @$result;打印@$结果; (因为在那之后你没有使用它)或打印 $result->[$_] for 1..$#result; C 风格的 for 循环会出现错误,通常最好写成范围运算符。