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
, and only when using the system command.perl -e "..."
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; }
#!/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;
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(...)"
For those with sharp eyes, please note that there was no difference betweenD:\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
andmy $rc = system ("$cmd");
andmy $rc = system ($cmd);
UPDATE: As noted by ikegami, the warning does occur from the perl command line if I 'use warnings'.my $rc = system $cmd;
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.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |