Perl - 调用其他脚本并在运行时显示输出

时间:2014-02-26 03:32:57

标签: perl

我正在编写一个Perl脚本,询问一些输入,然后最终运行另一个脚本将这些输入作为参数传递。我正在调用的脚本具有print个输出,用于指定在进行foreach循环时处理完成的百分比。

目前我正在调用其他Perl脚本:

   print `perl /path/to/script.pl -x 0990;`

这样可以正常工作,但只在该被调用脚本完成时打印任何输出...例如,当该脚本完成时,我得到的输出是:

   Status: 100% Completed
   Done!

当我想获得每个%号的输出时。

任何可能的解决方案?

谢谢!

1 个答案:

答案 0 :(得分:1)

我希望这适合你:

#!/usr/bin/perl
use POSIX ":sys_wait_h";
use IPC::Open3;
use IO::Select;

# forces a flush to stdout
$| = 1;

# open a process for writing, reading, and error
my $pid = open3(0,\*READ,0,"perl /path/to/script.pl -x 0990");

# reading process output in non-blocking mode
my $sel = new IO::Select();
$sel->add(\*READ);

# read process output until process returns
do {
 foreach my $h ($sel->can_read)
 {
  my $buf = '';
  sysread(READ,$buf,4096);
  if($buf){print "response->$buf"}
 }
 $kid = waitpid(-1, WNOHANG);
} while $kid > -1;

<强>编辑:

此脚本无法在Windows平台上运行