【问题标题】:How can I get forking pipes to work in Perl on Windows?如何让分叉管道在 Windows 上的 Perl 中工作?
【发布时间】: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 循环会出现错误,通常最好写成范围运算符。

标签: windows perl fork pipe


【解决方案1】:

我知道这不是对您问题的直接回答,但看起来您是在 Perl 的 Perforce 之上编写一些脚本?如果是这样,您可能会发现现有的库已经完成了您想要的工作,并且为自己省去了很多麻烦,或者至少给了您一些示例代码来工作。

例如:

编辑:现在我知道你在做什么,我猜你正在尝试将 p42svn 移植到 Windows,或者至少让它与 Windows 兼容。请参阅this thread 了解有关此问题的讨论。建议(未经测试)尝试使用 http://perldoc.perl.org/perlfork.html 中“Forking pipe open() not yet implemented”下列出的代码示例来显式创建管道。 p>

【讨论】:

  • 是的,我正在尝试将 Perforce 数据库迁移到 Subversion 数据库。但是,地球上只有一个已知的脚本可以做到这一点,而且该脚本只能在 UNIX 上运行,这对我来说不是一个选择。我正在尝试将脚本移植到 Windows 上运行。在今天之前,我从未接触过 PERL
【解决方案2】:

它不会按原样工作。你需要找到另一种方法来完成它正在做的事情。看起来不需要叉管,但很难说,因为我不知道 p4 是什么,而且你的很多代码都被尖括号解释丢失了。

【讨论】:

  • 是的,我相信 fork-pipe 用于处理二进制文件和文本文件,因为 print() 的行为不一致。这是一个关于这个问题的线程,但是还没有人解决它:p42svn.tigris.org/ds/…
猜你喜欢
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
  • 2016-09-19
  • 1970-01-01
  • 2017-05-13
  • 2011-06-10
相关资源
最近更新 更多