Also see system command can't spawn cmd.exe

On more than one occasion, I have received the dreaded dos message Can't spawn <executable> after executing a perl command

my $ret = system(executable);

Sometimes the error message refers to "cmd.exe" even though I am trying to run another executable.

Well, one time (once too many) I knew that since my perl program had been happily working for many months, and now wasn't (after i modified the executable in question), it had to do with the executable, not with perl.

So, with a little experimentation, I have discovered that this system error occurs if the executable returns with a negative return code (at least on WinNT, and definately not on solaris).

It only happens when called from a perl script, and not from a

perl -e "..."
, and only when using the system command.

For example, with the following c-code program

#include "stdio.h" int main(int argc, char* argv[]) { int rc,i; printf("Hello World!\n"); if (argc > 1) { sscanf(argv[1],"%d",&rc); printf("Returning with code %d\n",rc); i = 10/rc; printf ("i = %d\n",i); return rc; } printf ("Return with code 0\n"); return 0; }
and the following perl code
#!/usr/bin/perl use strict; use warnings; my $input = shift || ""; my $cmd = "rcs_c.exe $input"; print "\n\n--- Executing cmd '$cmd' via system\n\n"; my $rc = system $cmd; print "--- Execution finished with return code: ",$rc>>8,"\n"; print "\n\n--- Executing cmd '$cmd' via backticks\n"; my $return = `$cmd`; print "--- Execution finished, and returned:\n\n$return\n"; print "\n\nbye...\n"; exit $rc>>8;
I can execute my script passing a parameter which in turn will be passed to the c-code. This parameter will be used as the return code from within the c-program.

Note that with a return code of zero or a positive number, there is no error message, but when the return code is negative (any negative number gives same return code), the c-executable still executes, but prints that annoying error on the screen.

Important note: Even though there is that annoying message that initially had me convinced something was very wrong, it turned out (at least in these cases) that my executable executed quite happily, and everything was a-ok.

Note also that this does not occur if I execute the system command via a perl -e "system(...)"

D:\rcs>perl rcs.pl 0 --- Executing cmd 'rcs_c.exe ' via system Hello World! Return with code 0 --- Execution finished with return code: 0 --- Executing cmd 'rcs_c.exe ' via backticks --- Execution finished, and returned: Hello World! Return with code 0 bye... D:\rcs>perl rcs.pl -1 --- Executing cmd 'rcs_c.exe -1' via system Hello World! Returning with code -1 i = -10 Can't spawn "rcs_c.exe -1": No error at rcs.pl line 8. --- Execution finished with return code: 255 --- Executing cmd 'rcs_c.exe -1' via backticks --- Execution finished, and returned: Hello World! Returning with code -1 i = -10 bye... D:\rcs>perl -e "system(qq(rcs_c.exe -1))" Hello World! Returning with code -1 i = -10
For those with sharp eyes, please note that there was no difference between
my $rc = system ("$cmd");
and
my $rc = system ($cmd);
and
my $rc = system $cmd;
UPDATE: As noted by ikegami, the warning does occur from the perl command line if I 'use warnings'.

Also: If I change the system command to redirect the output, it does indeed say 'Can't spawn "cmd.exe"'.

So, now that I know that a negative return code from an executable causes DOS to barf up an error/warning message, I can deal with it.


In reply to system command erroneously states 'can't spawn <executable>' by Sandy

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.