Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Open
tlbdk opened this Issue · 4 comments

3 participants

@tlbdk
# 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 @_;    
};
@schwern
Owner

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

@tlbdk

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";

@daxim

That's easy to solve, see tchrist's boilerplate use of sigtrap at http://stackoverflow.com/questions/4354097/perl-full-screen-scrolling and http://stackoverflow.com/questions/4355175/remove-lines-contaning-non-ascii-characters-from-a-file-in-perl. END { close STDOUT } is good too, so you also get an exception for that type of rare failure instead of exiting silently.

@schwern
Owner

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.