in reply to Re: system and $?
in thread system and $?
You haven't mentioned what $! say
In this posting, I was mainly interested why system() does not adhere to the docs in that it does not return -1 on some platforms even the execution failed.
But - and maybe I should have mentioned this in my original post - the interest in this issue arose from a discussion with a co-worker about the proper usage of $!. My co-worker argued that it is never necessary to explicitly set $!=0 in a Perl program. His argument sounded convincing: $! (which is just errno) is only set if a low-level system call encounters a problem; otherwise, the value is not changed. Therefore, his argument goes, we first need to check if a function was successful, and only if it is not, we investigate $!. Example:
This sounded convincing, but for the safe side, I tried to find a counter example, and came up with the example of system('myprog.exe'), where I can't, without prior setting of $!=0, reliably distinguish, whether or not myprog.exe has been executed with an exit code of 1, or could not be executed because it does not exist:if(open(FOO,'<',$foo)) { ... } else { print "open $foo: $!\n"; }
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. But if myprog.exe can be run, and exits with code 1, I also get 256 as return code, and $! has an undefined value (it is set to whatever it had been set before the call to system). Only by explicitly setting it to 0 prior to running system(), is it that I can distinguish between those cases.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: system and $?
by Limbic~Region (Chancellor) on Aug 26, 2009 at 13:03 UTC | |
by rovf (Priest) on Aug 26, 2009 at 13:19 UTC | |
by Limbic~Region (Chancellor) on Aug 26, 2009 at 13:22 UTC | |
|
Re^3: system and $?
by ikegami (Patriarch) on Aug 26, 2009 at 13:59 UTC | |
by Anonymous Monk on Aug 26, 2009 at 14:07 UTC | |
by ikegami (Patriarch) on Aug 26, 2009 at 14:12 UTC | |
by rovf (Priest) on Aug 26, 2009 at 14:18 UTC | |
by ikegami (Patriarch) on Aug 26, 2009 at 14:22 UTC |