Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl-“;“三通”;qx算子_Perl_Redirect - Fatal编程技术网

perl-“;“三通”;qx算子

perl-“;“三通”;qx算子,perl,redirect,Perl,Redirect,我正在编写一个脚本,目前有: my @files = `$some_command`; print @files; chomp @files; foreach my $file (@files) { process($file); } 它工作正常,但是some_命令部分占用了脚本的大部分时间。在这段时间内,stdout上没有显示任何内容,因为Perl已经重定向了some_命令的输出,以填充@文件数组。只有当some_命令完成并且Perl转到print@文件时,才会打印它 是否有一些聪明

我正在编写一个脚本,目前有:

my @files = `$some_command`;
print @files;
chomp @files;
foreach my $file (@files)
{
    process($file);
}
它工作正常,但是
some_命令
部分占用了脚本的大部分时间。在这段时间内,stdout上没有显示任何内容,因为Perl已经重定向了
some_命令
的输出,以填充
@文件
数组。只有当
some_命令
完成并且Perl转到
print@文件时,才会打印它

是否有一些聪明的方法来更改此代码,以便在执行时显示
some_命令的输出?我可以用
tee(1)
试试这样的东西:

my$tmpfile=File::Temp->new();
系统($some_命令| tee.$tmpfile->filename);
我的@文件;
{local$/=unde;@files=split/\s/,;}

但是,如果有更简单的解决方案,我宁愿避免使用临时文件。

该模块看起来可以满足您的需要。在运行
system()
时,有一些重定向标准输出的示例。我没有使用它,所以我不能给出更多具体的例子,但看起来这将是一个很好的起点。

您可以在打印行的同时打开句柄并手动填充数组

像这样的事情可能会奏效

  open my $fh, '-|', $some_command;
  while(<$fh>)
  {
    print $_;
    push @files, $_;
  }
  close $fh;
打开我的$fh,'-|',$some_命令;
while()
{
打印美元;
推送@files,$\ux;
}
收盘价$fh;

您可以跳过
qx()
操作符,直接打开进程输出流的文件句柄。此代码在功能上相当于
my@files=qx($some_命令)


一个重要的考虑因素是
$some\u命令的输出缓冲行为。如果该命令缓冲其输出,则在有大数据块可用之前,
$proc\u fh
句柄将不会接收任何输入。

您也可以将命令作为文件描述符打开,并在命令生成输出时读取该输出。类似这样的内容(摘自):

#/usr/bin/perl
打开(PINGTEST,“/bin/ping-c5 netadmintools.com”;”;
$i=1;
而(){
打印“行#”。$i.“”.$\u;
$i++;
}
打印“全部完成!\n”;
闭合试验;
可能正是您想要的

   use Capture::Tiny qw/ tee tee_merged /;

   ($stdout, $stderr) = tee {
     # your code here
   };

   $merged = tee_merged {
     # your code here
   };

您是否尝试过取消缓冲输出?$|=1.
 my @files = ();
 open my $proc_fh, "$some_command |";
 while (<$proc_fh>) {
     push @files, $_;
 }
 close $proc_fh;
 while (<$proc_fh>) {
     print "INPUT: $_\n";
     push @files, $_;
 }
#!/usr/bin/perl
open (PINGTEST, "/bin/ping  -c 5 netadmintools.com |");
$i=1;
while (<PINGTEST>){
print "Line # ".$i." ".$_;
$i++;
}
print "All done!\n";
close PINGTEST;
   use Capture::Tiny qw/ tee tee_merged /;

   ($stdout, $stderr) = tee {
     # your code here
   };

   $merged = tee_merged {
     # your code here
   };