You can distinguish between a successfully run command returning 1 and a failed command by checking $^E.

If $^E is empty, the command ran successfully and the contents of $? are the lower 8 bits << 8 of the 16-bit value returned by the executable.

If $^E is set, usually to The system cannot find the file specified, then the command could not be found--either directly, nor by running cmd.exe passing the parameter(s) supplied on the system call as argument(s) in the hope it can be resolved via the path--and $? contains the failure code (1) from cmd.exe.

This is when the ...' is not recognised as an internal or external command message is produced. This can be suppressed by redirecting stderr to null as in the 3rd example below:

#! perl -slw use strict; print 'Successful command returning 1'; system 'c:/perl/bin/perl.exe -e"sleep 5; exit 1"'; print "![$!] ?[@{[$?, ':', $?>>8]}] E[$^E]"; print "\nNonexistant command, attempted direct but fallback to via cmd +"; system 'c:/doesNotExists.exe'; print "![$!] ?[@{[$?, ':', $?>>8]}] E[$^E]"; print "\nNonexistant via cmd because of the presence of shell chars se +nding stderr >null"; system 'c:/doesNotExists.exe 2>null'; print "![$!] ?[@{[$?, ':', $?>>8]}] E[$^E]"; __END__ c:\test>junk Successful command returning 1 ![Bad file descriptor] ?[256 : 1] E[] Nonexistent command, attempted direct but fallback to via cmd 'c:/doesNotExists.exe' is not recognized as an internal or external co +mmand, operable program or batch file. ![No such file or directory] ?[256 : 1] E[The system cannot find the f +ile specified] Nonexistent via cmd because of the presence of shell chars sending std +err >null ![No such file or directory] ?[256 : 1] E[The system cannot find the f +ile specified]

The real problem here is that the win32 implementation of the posix system call attempts to emulate that call as best it can.

So, for example, return codes from executables above 255 get truncated to 8-bits and shifted left to leave room for signal values, which you can never receive.

And even if you supply multiple arguments to system, if the first attempt to run the command directly fails, it defaults to trying again, by supplying the commands you supply, to 'cmd.exe' to see if it can find the command.

This is not an exhaustive explanation of what goes on in Win32.c. I seem to recall tye posting a pretty comprehensive breakdown, but I can't persuade google to find it for me.

You might also want to look into using SetErrorMode() to (optionally) suppress system popups for things like segfaults and 'Insert disk for drive x:' and similar..


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: $? set to strange values on failure under Win32 by BrowserUk
in thread $? set to strange values on failure under Win32 by pjf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.