in reply to How to skip to next Command line

system waits for its external command to complete. If you want to continue executing the script oblivious of the progress of the system call, you will have to fork off your system call. Here's a cheap and dirty example:

if( fork() == 0 ) { system( "C:\\block.exe" ) or die "Command failed: $!\n"; exit; } print "processing second line...\n";

It's important to keep track of who is the parent and who is the child. fork() returns '0' to the child process, and the child process's ID to the parent. So the if(){} test I've used detects whether a given process is the child (in which case it executes the system command and then exits), or the parent (in which case it continues on to the next line). I could have used exec instead of system, but wanted to provide a way to gracefully handle external command failure. See fork, perlfork, and in a broader sense, perlipc for additional info and discussion of some of the fork pitfalls and handling processes.


Dave

Replies are listed 'Best First'.
Re^2: How to skip to next Command line
by Perl Mouse (Chaplain) on Jan 25, 2006 at 09:35 UTC
    The problem with that solution is that if the fork fails, block.exe will be executed, but the rest of the program won't. Also, system returns false if the command was succesful, and any error status is in $?, not $!.

    A better way would be:

    my $pid = fork; die "Fork failed: $!" unless defined $pid; unless ($pid) { system "C:\\block.exe"; die "Command failed with exit code ", $? >> 8 if $?; exit; } print "processing second line...\n";
    Or, if you don't want to handle failures of block.exe in your program:
    my $pid = fork; die "Fork failed: $!" unless defined $pid; unless ($pid) { exec "C:\\block.exe"; die "exec failed: $!"; } print "processing second line...\n";
    Perl --((8:>*