in reply to system and $?

To get the return value of the program, you need to check $? & 127:
system "true"; echo $? & 127;
If you need the "result" of the system command, get the return value:
print system "does-not-exist"; print system "true"; print system "false";
The results are -1, 0 and 256. Not really good, but here you get -1 in case of any kind of execution error.
I agree that this behaviour isn't good at all and the documentation doesn't seem to match the reality. Sorry for this.

Replies are listed 'Best First'.
Re^2: system and $?
by rovf (Priest) on Aug 26, 2009 at 12:35 UTC
    you get -1 in case of any kind of execution error.

    Not on Windows 2000, as you can see from my example. I don't know about XP or Vista. Maybe someone could try this out?

    BTW, to get the return code on Windows 2000 (ActiveState Perl again - I don't know about Strawberry), you need to do a bit more logic than you posted, because you can't distinguish between a program execution error, and a program which runs fine but returns exit code 1. Here is what you have to do:

    $!=0; system('your_program_goes_here'); my $child_error=$? & 0xff; my $maybe_exit_code=$? >> 8; if($child_error || ($maybe_exit_code && $!)) { # there was an error in program execution }
    Also note that even if $?==0, it does not say that your program terminated with exit code 0. You get only the low 8 bit of the "real" Windows exit code, so if your program exited with a code which is a whole multiple of 256, you wouldn't notice it.
    -- 
    Ronald Fischer <ynnor@mm.st>