I am working on a script that currently has:
my @files = `$some_command`;
print @files;
chomp @files;
foreach my $file (@files)
{
process($file);
}
It works correctly, but the part some_commandtakes most of the time script. And during this time nothing appears on stdout, because Perl redirected the output from some_commandto fill the array @files. It is printed only when it some_commandis executed, and Perl switches to print @files;.
Is there any smart way to change this code so that the result some_commandappears as it executes? I could try something like this using tee(1):
my $tmpfile = File::Temp->new();
system("$some_command | tee " . $tmpfile->filename);
my @files;
{ local $/ = undef; @files = split /\s/, <$tmpfile>; }
But I would prefer to avoid a temporary file if there is a simpler solution.
source
share