Tom Tanner Tom Tanner - 5 months ago 15
Perl Question

How come my fork/exec and close is losing the error code from the child

I have a piece of perl code that is roughly like this

my $pid = open(PIPE, '-|');
die "Unable to fork: $!\n" if !defined $pid;
if ($pid == 0)
open STDERR, '>&STDOUT' or die "Can't redirect STDERR to STDOUT\n";
die "Unexpected exec failure: $!\n";
my @lines = (<PIPE>);
close PIPE;
if ($? != 0) { do stuff; }

However, for reasons I can't understand, possibly related to the actual program being called not existing, sometimes this fails to pick up the error from the child in

contains the "Unexpected exec failure: File or directory does not exist" as expected (and some other output from a
handler), but
is set to 0. I'm working round this for now by also checking the return from
which is fortunately set to 1. But where did my error code go?


Well, I found what it was.

Someone had added an END{} block in a library that was destroying the return codes (by calling system and hence destroying $?). I backed out the change and everything was happy.