Consider the following script:
use IO::File;
$| = 1;
my ($handle, $pid) = myPipe();
if ($pid == 0) {
print "$$";
sleep 5;
exit;
}
print "child: ".<$handle>."\n";
sub myPipe {
my $handle = new IO::File();
my $pid = open($handle, "-|");
return ($handle, $pid);
}
In this case, the "child:" message doesn't appear for 5 seconds after the process starts. If I remove the sleep call from the forked child, then it prints immediately. Why does the forked child have to exit for the pipe to flush to the parent?