6

我需要从 perl 调用一些 shell 命令。这些命令需要相当长的时间才能完成,所以我想在等待完成时查看它们的输出。

系统功能在完成之前不会给我任何输出。

exec函数给出输出;但是,它从那时起退出了 perl 脚本,这不是我想要的。

我在 Windows 上。有没有办法做到这一点?

4

1 回答 1

17

Backticksqx命令在单独的进程中运行命令并返回输出:

print `$command`;
print qx($command);

如果您希望查看中间输出,请使用open创建命令输出流的句柄并从中读取。

open my $cmd_fh, "$command |";   # <---  | at end means to make command 
                                 #         output available to the handle
while (<$cmd_fh>) {
    print "A line of output from the command is: $_";
}
close $cmd_fh;
于 2010-12-14T20:14:24.620 回答