从Perl调用命令,需要查看输出

cut*_*CAT 6 perl shellexecute

我需要从perl调用一些shell命令.这些命令需要相当长的时间才能完成,因此我希望在等待完成时看到它们的输出.

系统直到它完成的功能并没有给我任何的输出.

EXEC函数给出输出; 但是,它从那一点退出perl脚本,这不是我想要的.

我在Windows上.有没有办法实现这个目标?

mob*_*mob 15

反引号qx命令在单独的进程中运行命令并返回输出:

print `$command`;
print qx($command);
Run Code Online (Sandbox Code Playgroud)

如果希望看到中间输出,请使用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;
Run Code Online (Sandbox Code Playgroud)

  • ++用于反叛和管道打开 - 我唯一的挑剔是没有提到该技术的3+ args版本,这应该更安全一些:打开我的$ cmd_fh,' - |',$ command; 另外,链接:http://perldoc.perl.org/perlopentut.html#Pipe-Opens (4认同)