in reply to Re^2: system and $?
in thread system and $?

If myprog.exe can not be run, system(...) returns on my platform 256 (which is exit code 1, no child-error), and, as you point out, sets $! to some error code.

You have a broken Perl if that's true.

If myprog can not be run, system sets $? to -1 and sets the error in $!.

If a shell command cannot be run, system sets $? to something other than -1 (since the shell was successfully executed) and $! contains no meaningful information. The shell cannot possibly change the parent's errno.

Replies are listed 'Best First'.
Re^4: system and $?
by Anonymous Monk on Aug 26, 2009 at 14:07 UTC
    By that logic, all windows perls are broken, which may or may not be true.

      It is broken in the sense that the shell is needlessly invoked. It probably does that because people expect `dir` to work. (dir is a shell command, not an executable.)

      But it does behave correctly for the shell being invoked.

      >perl -le"system 'foo'; print $?==-1 ? qq{$!=\$!} : qq{\$?=$?}" 'foo' is not recognized as an internal or external command, operable program or batch file. $?=256

      Unfortunately, that's all the information to which you have access. $! is meaningless. Conveniently, it does print an error message to STDERR.

      Update: This is even odder:

      >perl -le"system({ 'foo' } 'foo'); print $?==-1 ? qq{$!=\$!} : qq{\$?= +$?}" $?=65280
Re^4: system and $?
by rovf (Priest) on Aug 26, 2009 at 14:18 UTC
    You have a broken Perl if that's true.

    Well, it's pretty easy to reproduce, and the effect is the same in ActiveState 5.8.8 und 5.10. If I find the time, I'll try it with Strawberry...

    The shell cannot possibly change the parent's errno.
    Certainly not. It would need to communicate the information in a different way to the parent process. Well, this is a theoretical question, because Perl - likely in order to be portable - is not programmed that way, but: If it would be possible on Windows to somehow get the full (i.e. 32 Bit) Exitcode, we could use this information: If the shell can't execute a program, it sets this exitcode to 9009.

    OTOH, this would still make it indistinguishable from a program which deliberately sets that exit code..

    -- 
    Ronald Fischer <ynnor@mm.st>

      Well, it's pretty easy to reproduce, and the effect is the same in ActiveState 5.8.8 und 5.10.

      I used ActivePerl 5.10.0 b1005 above. 5.8.8 gives the same result. Both successfully execute the shell, so $? is not -1, so $! is meaningless.

      OTOH, this would still make it indistinguishable from a program which deliberately sets that exit code..

      Yes.