Mike Mike - 1 year ago 65
Perl Question

Why is Perl's $? returning the wrong value for the exit code of a forked process?

Consider this trivial example of fork()ing then waiting for a child to die in Perl:


use strict;
use warnings;

if (fork() == 0) {


print $?;

Running the script on Solaris 10 I get this result:

$ perl test.pl

I suspect the values of are being shifted upwards because when I do
in the child, the output becomes

I can't seem to find this documented in perl's waitpid. Is this a bug on my system or am I doing something wrong?

Answer Source

It's documented in the $? section of the perlvar man page.

i.e. the real exit code is $? >> 8.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download