我需要从 perl 调用一些 shell 命令。这些命令需要相当长的时间才能完成,因此我想在等待完成时查看它们的输出。
系统函数在完成之前不会给我任何输出。
exec函数给出输出;但是,它从那时起退出 perl 脚本,这不是我想要的。
我使用的是 Windows。有办法实现吗?
最佳答案
Backticks或 qx
命令,在单独的进程中运行命令并返回输出:
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;
关于perl - 从 Perl 调用命令,需要查看输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4443659/