- system LIST
- system PROGRAM LIST
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. If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list. If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is/bin/sh -c
on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly toexecvp
, which is more efficient.Beginning with v5.6.0, Perl will attempt to flush all files opened for output before any operation that may do a fork, but this may not be supported on some platforms (see perlport ). To be safe, you may need to set
$|
($AUTOFLUSH in English) or call theautoflush()
method ofIO::Handle
on any open handles.The return value is the exit status of the program as returned by the
wait
call. To get the actual exit value, shift right by eight (see below). 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 orqx//
, as described in ""`STRING`"" in perlop . Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).Like
exec
,system
allows you to lie to a program about its name if you use thesystem PROGRAM LIST
syntax. Again, see "exec" .Since
SIGINT
andSIGQUIT
are ignored during the execution ofsystem
, if you expect your program to terminate on receipt of these signals you will need to arrange to do so yourself based on the return value.@args = ("command", "arg1", "arg2");
system (@args) == 0
or die "system @args failed: $?"You can check all the failure possibilities by inspecting
$?
like this:if ($? == -1) {
print "failed to execute: $!/n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump/n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d/n", $? >> 8;
}Alternatively you might inspect the value of
${^CHILD_ERROR_NATIVE}
with the W*() calls of the POSIX extension.When the arguments get executed via the system shell, results and return codes will be subject to its quirks and capabilities. See ""`STRING`"" in perlop and "exec" for details.
- exec LIST
- exec PROGRAM LIST
The
exec
function executes a system command and never returns -- usesystem
instead ofexec
if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).Since it's a common mistake to use
exec
instead ofsystem
, Perl warns you if there is a following statement which isn'tdie
,warn
, orexit
(if-w
is set - but you always do that). If you really want to follow anexec
with some other statement, you can use one of these styles to avoid the warning:exec ('foo') or print STDERR "couldn't exec foo: $!";
{ exec ('foo') }; print STDERR "couldn't exec foo: $!";If there is more than one argument in LIST, or if LIST is an array with more than one value, calls execvp(3) with the arguments in LIST. If there is only one scalar argument or an array with one element in it, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is
/bin/sh -c
on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly toexecvp
, which is more efficient. Examples:exec '/bin/echo', 'Your arguments are: ', @ARGV;
exec "sort $outfile | uniq";If you don't really want to execute the first argument, but want to lie to the program you are executing about its own name, you can specify the program you actually want to run as an "indirect object" (without a comma) in front of the LIST. (This always forces interpretation of the LIST as a multivalued list, even if there is only a single scalar in the list.) Example:
$shell = '/bin/csh';
exec $shell '-sh'; # pretend it's a login shellor, more directly,
exec {'/bin/csh'} '-sh'; # pretend it's a login shell
When the arguments get executed via the system shell, results will be subject to its quirks and capabilities. See ""`STRING`"" in perlop for details.
Using an indirect object with
exec
orsystem
is also more secure. This usage (which also works fine with system()) forces interpretation of the arguments as a multivalued list, even if the list had just one argument. That way you're safe from the shell expanding wildcards or splitting up words with whitespace in them.@args = ( "echo surprise" );
exec @args; # subject to shell escapes
# if @args == 1
exec { $args[0] } @args; # safe even with one-arg listThe first version, the one without the indirect object, ran the echo program, passing it
"surprise"
an argument. The second version didn't--it tried to run a program literally called "echo surprise" , didn't find it, and set$?
to a non-zero value indicating failure.Beginning with v5.6.0, Perl will attempt to flush all files opened for output before the exec, but this may not be supported on some platforms (see perlport ). To be safe, you may need to set
$|
($AUTOFLUSH in English) or call theautoflush()
method ofIO::Handle
on any open handles in order to avoid lost output.Note that
exec
will not call yourEND
blocks, nor will it call anyDESTROY
methods in your objects.conclusion:
Both Perl's exec() function and system() function execute a system shell command. The big difference is that system() creates a fork process and waits to see if the command succeeds or fails - returning a value. exec() does not return anything, it simply executes the command. Neither of these commands should be used to capture the output of a system call. If your goal is to capture output, you should use the backtick operator :
$result = `PROGRAM`;