STDOUT到数组Perl

gth*_*thm 2 perl stdout

我正在编译一个Perl程序,我正在将输出STDOUT写入文件.在同一个程序中,我想在STDOUT的输出上使用while函数运行另一个小脚本.所以,我需要将第一个脚本的输出保存在一个数组中,然后我可以在<@array>中使用.喜欢

open(File,"text.txt");
open(STDOUT,">output,txt");
@file_contents=<FILE>;

foreach (@file_contents){

//SCRIPT GOES HERE//

write;
}
format STDOUT =
VARIABLE    @<<<<<< @<<<<<<  @<<<<<<
             $x      $y       $z
.

//Here I want to use output of above program in while loop //

while(<>){

}
Run Code Online (Sandbox Code Playgroud)

如何将第一个程序的输出保存到数组中以便我可以在while循环中使用,或者如何在while循环中直接使用STDOUT.我必须确保第一部分完全执行.提前致谢.

Jon*_*ler 6

由于您重新映射STDOUT以便写入文件,因此可能会关闭STDOUT,然后重新打开文件以进行读取.

您要发送任何其他输出的地方有点神秘,但可能您可以解决这个问题.如果是我,我不会乱搞STDOUT.我让脚本写入文件句柄:

use strict;
use warnings;

open my $input, "<", "text.txt" or die "A horrible death";
open my $output, ">", "output.txt" or die "A horrible death";
my @file_contents = <$input>;
close($input);

foreach (@file_contents)
{
    # Script goes here
    print $output "Any information that goes to output\n";
}
close $output;

open my $reread, "<", "output.txt" or die "A horrible death";

while (<$reread>)
{
    # Process the previous output
}
Run Code Online (Sandbox Code Playgroud)

注意使用词法文件句柄,检查是否正常open工作,close何时完成输入文件,使用use strict;use warnings;.(我只和Perl一起工作了20年,我知道我不相信我的脚本,直到他们用这些设置运行干净.)