NAME

system - run a separate program


SYNOPSIS

system LIST


DESCRIPTION

Does exactly the same thing as ``exec LIST'' except that a fork is done first, and the parent process waits for the child process to complete. Note that argument processing varies depending on the number of arguments. The return value is the exit status of the program as returned by the wait() call. To get the actual exit value divide by 256. See also exec. This is NOT what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in `STRING`.

Because system() and backticks block SIGINT and SIGQUIT, killing the program they're running doesn't actually interrupt your program.

    @args = ("command", "arg1", "arg2");
    system(@args) == 0
         or die "system @args failed: $?"

Here's a more elaborate example of analysing the return value from system() on a Unix system to check for all possibilities, including for signals and core dumps.

    $rc = 0xffff & system @args;
    printf "system(%s) returned %#04x: ", "@args", $rc;
    if ($rc == 0) {
        print "ran with normal exit\n";
    }
    elsif ($rc == 0xff00) {
        print "command failed: $!\n";
    }
    elsif ($rc > 0x80) {
        $rc >>= 8;
        print "ran with non-zero exit status $rc\n";
    }
    else {
        print "ran with ";
        if ($rc &   0x80) {
            $rc &= ~0x80;
            print "core dump from ";
        }
        print "signal $rc\n"
    }
    $ok = ($rc != 0);

When the arguments get executed via the system shell, results will be subject to its quirks and capabilities. See `STRING` for details.