note
ikegami
<p>You call <c>waitpid</c> after you've reaped the process, causing causing an error (<c>$? = -1;</c>, error message in <c>$!</c>).
<p>You gotta check <c>$?</c> after the successful call to <c>waitpid</c>.
<c>
use POSIX qw( WNOHANG );
my $pid = system(1,'perl -e"sleep(4); exit(5);"');
while (1) {
my $wait = waitpid($pid, WNOHANG);
die $! if $wait < 0;
last if $wait > 0;
}
print "Command completed with exit code $?.\n";
</c>
<p>I presume you actually do something else in the loop? If not, there's no reason to use WHOHANG.
<c>
my $pid = system(1,'perl -e"sleep(4); exit(5);"');
waitpid($pid, 0);
die $! if $? < 0;
print "Command completed with exit code $?.\n";
</c>
1071355
1071358