in reply to Exit codes from forking children

I bet the child gets killed by a SIGPIPE (13) in the gzip case.

See system for how to properly interpret $? to also catch such errors (the signal number is in the lower byte which you're just "shifting out" by unconditionally doing $? >> 8).

Replies are listed 'Best First'.
Re^2: Exit codes from forking children
by ikegami (Patriarch) on Dec 04, 2009 at 00:05 UTC
    Good call.
    defined( my $pid = open(CMD, '-|') ) or die("Can't fork: $!\n"); if ($pid == 0) { close(STDERR); open(STDERR, ">&STDOUT") or print "Unable to reopen STDOUT, errors may be lost: $!"; exec('/bin/tar', 'czf', '/nonexistent/test.tar.gz', '.') or die("Unable to exec: $!") } local $/ = undef; my $output .= <CMD>; close(CMD); if ($? == -1) { die("Can't close pipe: $!\n"); } elsif ($? & 127) { die("tar died from signal ", ($? & 127), "\n"); } elsif ($? >> 8) { die("tar exited with error ", ($? >> 8), "\n"); } print $output;
    tar died from signal 13