in reply to detecting non-zero exit code when opening from a pipe

Full error checking and safer argument handling:

open(my $fh, '-|', 'gunzip', '-c', '--', $input_file) or die("Can't start gunzip: $!\n"); while (!eof($fh)) { defined($_ = <$fh>) or die("Error reading from gunzip: $!\n"); ... } close($fh) or die("Can't close gunzip: $!\n"); ($? & 0x7F) and die("gunzip died from signal ", ($? & 0x7F), "\n"); ($? >> 8) and die("gunzip exited with error ", ($? >> 8), "\n");

close

Replies are listed 'Best First'.
Re^2: detecting non-zero exit code when opening from a pipe
by Anonymous Monk on Oct 21, 2010 at 21:28 UTC

    Hmm... didn't quite trap at the open statement.

    >./errorcheck.pl notexists.gz gzip: notexists.gz: No such file or directory Can't close gunzip:

    I guess the "Can't close gunzip" occurred due to a broken pipe?

      Of course it didn't trap at the open statement. The error hasn't happened yet. As for the incorrect error message, the code should be:

      ... if (!close($fh)) { $! and die("Can't close gunzip: $!\n"); ($? & 0x7F) and die("gunzip died from signal ", ($? & 0x7F), "\n"); ($? >> 8) and die("gunzip exited with error ", ($? >> 8), "\n"); }
      gzip: notexists.gz: No such file or directory gunzip exited with error 1