Problems? Is your data what you think it is? | |
PerlMonks |
perlfunc:execby gods (Initiate) |
on Aug 24, 1999 at 22:42 UTC ( [id://196]=perlfunc: print w/replies, xml ) | Need Help?? |
execSee the current Perl documentation for exec. Here is our local, out-dated (pre-5.6) version: exec - abandon this program to run another
exec LIST exec PROGRAM LIST
The exec() function executes a system command AND NEVER RETURNS - use system() instead of exec() 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 of system(), Perl warns you if there is a following statement which isn't die(), warn(), or exit() (if
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
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 shell or, 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` for details. Using an indirect object with exec() or system() is also more secure. This usage 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" );
system @args; # subject to shell escapes # if @args == 1 system { $args[0] } @args; # safe even with one-arg list
The first version, the one without the indirect object, ran the echo
program, passing it
Note that exec() will not call your |
|