从 Perl 调用命令,需要查看输出 [英] Calling command from Perl, need to see output

查看:66
本文介绍了从 Perl 调用命令,需要查看输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to call some shell commands from perl. Those commands take quite some time to finish so I'd like to see their output while waiting for completion.

system 函数在完成之前不会给我任何输出.

The system function does not give me any output until it is completed.

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

The exec function gives output; however, it exits the perl script from that point, which is not what I wanted.

我使用的是 Windows.有没有办法做到这一点?

I am on Windows. Is there a way to accomplish this?

推荐答案

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

Backticks, or the qx command, run a command in a separate process and returns the output:

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

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

If you wish to see intermediate output, use open to create a handle to the command's output stream and read from it.

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;

这篇关于从 Perl 调用命令,需要查看输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆