Is the value returned by system() the same as "$?"?

Viewed 199

When I do system() calls in Perl, I usually inspect the return code according to the perldocs. Well, I thought so. Most of the time $rc!=0 is sufficient for me. Recently I helped two people here which had trouble with system() calls when running their .cgi scripts under apache. I instructed them to examine the $rc of

my $rc = system(...);

and linked them to the system() docs. Then I looked closer and noticed the docs aren't really talking about the $rc but instead about $? and I felt a bit embarassed and the following question arose:

Is there a difference between:

system(...);
if ($? == -1) {
    print "failed to execute: $!\n";
}
elsif ($? & 127) {
    printf "child died with signal %d, %s coredump\n",
        ($? & 127),  ($? & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $? >> 8;
}

and

my $rc = system(...);
if ($rc == -1) {
    print "failed to execute: $!\n";
}
elsif ($rc & 127) {
    printf "child died with signal %d, %s coredump\n",
        ($rc & 127),  ($rc & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n", $rc >> 8;
}

Or, for short, is $rc equal to $? for system()?

I looked through the docs of system, wait, and $? but it's not quite clear to me. Did I do wrong for the last 15 years by using $rc?

1 Answers
Related