http://www.perlmonks.org?node_id=1071376


in reply to Re^2: Getting a return code from waitpid in Windows
in thread Getting a return code from waitpid in Windows

You call waitpid after you've reaped the process, causing causing an error ($? = -1;, error message in $!).

You gotta check $? after the successful call to waitpid.

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

I presume you actually do something else in the loop? If not, there's no reason to use WHOHANG.

my $pid = system(1,'perl -e"sleep(4); exit(5);"'); waitpid($pid, 0); die $! if $? < 0; print "Command completed with exit code $?.\n";