Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
there's probably a simple answer to this but I can't figure it out. I have a simple perl script that does this
my $cmd="dostuff.ksh"; my $res = system $cmd; if ($res != 0) { print "Job has failed\n"; }
Every time I run this  $res = -1 and I have no idea why.
If I run the ksh script from the shell $? = 0 e.g.
$unx1: ./dokshstuff $unx1: echo $? 0
Since I'm not backgrounding the command or doing anything other than would I would deem to be a straightforward system call it's baffling me why this is returning -1.
I also know that the ksh script is running when called from the perl script becuase it updates a file within the script.
I've also tried checking the $? from within the perl script and it returns -1 which can't be the case (or can it ?) if the script is running properly
Any help welcome

Replies are listed 'Best First'.
Re: system return -1
by ikegami (Patriarch) on Oct 22, 2010 at 15:17 UTC
    The documentation not only explains what it means using prose — "Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason)." — it also provides sample code.
    if ($rv == -1) { print "failed to execute: $!\n"; } ...

    (The code example actually checks $?, which will have the same value as what system returns.)

      I had read the documentation before posing the question.
      As far as Perl is concerned it is failing with "no child processes". This implies that the called script via the system fork is not exiting cleanly. This is not the case however becuase as demonstrated in the code I put forward the shell script is exiting with a 0 and it also contains an exit 0 in the script.
      I'm not sure how to overcome this problem becuase the documentation says
      an implicit fork (2) is done with system , backquotes, or opening a pr +ocess as a filehandle. Be very careful to end the child code with an +exit
      How do I stop it from producing a "no child process" error ?
        $! is "no child processes"? That would indicate you are reaping the children elsewhere. (It doesn't have anything to do with exiting cleanly or not.) Are you using $SIG{CHLD}='IGNORE';, wait or waitpid anywhere?
Re: system return -1
by Anonymous Monk on Oct 22, 2010 at 14:18 UTC
    The example unix run was meant to say
    $unx1: ./dostuff $unx1: echo $? 0
      3rd time lucky
      $unx1: ./dostuff.ksh $unx1: echo $? 0