My pipe (filehandle, socket) breaks (sometimes). I can reproduce it with the following code:
my $counter = 5;
alarm(1);
open(FH,"while(sleep 2); do date; done |") or die $!;
while (<FH>) { print; }
close(FH);
BEGIN {
$SIG{ALRM} = sub {
print "alarm!\n";
exit if --$counter == 0;
alarm(1);
};
}
Which will produce:
alarm!
alarm!
Thu Feb 7 11:46:29 EST 2013
alarm!
alarm!
alarm!
If I strace this process, I see that the spawned shell gets a SIGPIPE. However, the Perl process happily continues. How do I fix this?