Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Get some IO error signals to throw exceptions instead of uncatchable warnings #67

tlbdk opened this Issue · 4 comments

3 participants

# Needed when writing to a broken pipe 
$SIG{PIPE} = sub { # SIGPIPE
    croak "Broken pipe";

# Needed as sysread() throws warnings when STDIN gets closed by the child
$SIG{__WARN__} = sub {
    croak @_;    

Could you provide some sample code illustrating the problem? I'm not really familiar with the details of pipes.


A pipe error in Perl only calls exit and you get no error:

#$SIG{PIPE} = sub { croak "Broken pipe";};
open my $cmd, "|cat";
system("killall cat");
sleep 1;
eval { print {$cmd} "test"; }; # We die without an error
close $cmd;
print "We never get here\n";

If we have the $SIG{PIPE} section we get a nice exception we can handle.

For the $SIG{WARN}, if you try to read/write from a filehandle that only has one direction you get a warning instead of an error, so here WARN should properly be converted to a exception.

#$SIG{WARN} = sub { croak @_; };
pipe my $readerIN, my $writerIN or die;
eval { syswrite($readerIN, "hello", 100) };
print "syswrite: $@\n";
eval { sysread($writerIN, my $data, 100) };
print "sysread: $@\n";


That's easy to solve, see tchrist's boilerplate use of sigtrap at and END { close STDOUT } is good too, so you also get an exception for that type of rare failure instead of exiting silently.


I see. Well, I don't use pipes much, but I'm for upgrading warnings into trappable exceptions.

If you can do it without making other warnings fatal then go ahead. Also preferably without relying on a $SIG{__WARN__} which is all too likely to be overwritten.

Alternatively, can this be fixed more elegantly in a subclass of IO::Pipe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.