Wise monks,

I'm working on a number of improvements to IPC::System::Simple, a cross-platform module designed to take the headache out of calling system() and checking its returns. Unfortunately, I've hit a snag when it comes to the behaviour of system() under Windows.

The idea of IPC::System::Simple is that you can replace this (from perldoc -f system):

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

with this:

use IPC::System::Simple qw(run); run("some_command");

Currently this works very well, but it's currently got a small problem detecting errors on Win32 systems.

Put very simply, any call to system() under Windows will fall-back to using the shell if it can't execute the command directly. This occurs regardless of the number of arguments, or the presence or absence of shell metacharacters. The problem with using the shell is that on failure to start the given command it returns an exit value of 1, which is indistinguishable from a command successfully running and merely returning an exit value of 1.

The end result is that we can never really be sure if Window's system() has failed or not.

Interestingly enough, the exact same problem occurs under Unix, but Perl only uses the shell under Unix when it's called with a single argument, and then only when it contains metacharacters. When the Unix shell is used, we can't tell the difference between a command failing to start, and one that runs to completion and merely returns an exit value of 127.

I'm intending for IPC::System::Simple to always mimic the Unix behaviour of system, regardless of the platform used. In other words:

use IPC::System::Simple qw(run); run("foo"); # No meta-chars, shell never used run("bar | bar"); # Meta-chars, shell always used. run("foo", "bar", "<baz") # Multi-arg, shell never used.

In the above examples, the first and third examples would be passed to the shell under Win32 if foo was not in the current working directory and we used system() instead. Neither would use the shell under Unix and modern releases of Perl.

One advantage of bypassing the shell is that we can also return the full 16-bit exit code returned by Windows processes. When using system() this is truncated to 8 bits.

This RFC is intended primarily as a sanity check. I feel I have a compelling reason to deviate from Perl's behaviour of system() under Windows; the current implementation is inconsistent compared to its Unix counterpart, it fails in an ambiguous way, and loses bits of the return value. Can anyone find me a compelling argument not to do this?

Further references:

In reply to RFC: IPC::System::Simple 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.