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

Does syscall wait for the command to finish before continuing in my Perl script?

If so, is there a way to call it so that it doesn't?

  • Comment on Does syscall wait for the command to finish?

Replies are listed 'Best First'.
Re: Does syscall wait for the command to finish?
by pbeckingham (Parson) on Jun 19, 2004 at 18:51 UTC

    Yes, syscall waits for the program to exit, so that it's exit code can be returned from syscall to your program.

    If you would like the other program to execute and for your program to immediately continue without knowing the exit code of the other program, then you need a combination of fork and exec:

    exec ($command, $arg1, ...) unless fork; # Continue here.

Re: Does syscall wait for the command to finish?
by dave_the_m (Monsignor) on Jun 19, 2004 at 22:03 UTC
    Well, I think you mean system() rather than syscall(), but they both wait.

    Dave.

Re: Does syscall wait for the command to finish?
by hbo (Monk) on Jun 20, 2004 at 02:55 UTC
    If system sees shell metacharacters in the string it is given, in invokes a shell to do the work. (Otherwise it exec's the command directly.) You can therefore do stuff like: perl -e 'system("sleep 20&");print "Hi\n";' and you will see "Hi" right away. There are lots of other possibilities for asynchronous job control with Perl. There are man pages for perlfork and perlipc that are helpful. The latter contains a discussion of the "forking form of open()" that is another way to get asynchronous command execution.