Subject: | Calling run3 garbles STDIN |
If run3 is called while reading from STDIN, some input will be lost.
Basically any scenario
while (<STDIN>)
{
...
run3 [ ... ], \undef, \$out, \$err;
...
}
causes some records to be lost or mutilated.
To demonstrate run the attached test file.
Subject: | stdin.t |
#!perl -w
use Test::More;
use IPC::Run3;
use File::Temp qw(tempfile);
use strict;
## test whether reading from STDIN is affected when
## run3 is called in between
# create a test file for input containing 1000 lines
my $nlines = 1000;
my @exp_lines;
my ($fh, $file) = tempfile(UNLINK => 1);
for (my $i = 1; $i <= $nlines; $i++)
{
my $line = "this is line $i";
push @exp_lines, $line;
print $fh $line, "\n";
}
close $fh;
# call run3 at different lines (any problem might manifest itself
# on different lines, probably due to different buffering of input)
my @try = (5, 10, 50, 100, 200, 500);
plan tests => @try * 2;
my ( $in, $out, $err );
foreach my $t (@try)
{
my $nread = 0;
my $unexpected;
open STDIN, "<", $file or die "can't open file $file: $!";
while (<STDIN>)
{
chomp;
$unexpected = qq[line $nread: expected "$exp_lines[$nread]", got "$_"\n]
unless $exp_lines[$nread] eq $_ || $unexpected;
$nread++;
if ($nread == $t)
{
run3 [ $^X, '-e', 'print q[something]' ], \undef, \$out, \$err;
die "command failed" unless $? == 0;
}
}
close STDIN;
is($nread, $nlines, "STDIN was read completely");
ok(!$unexpected, "STDIN as expected") or diag($unexpected);
}