Friday, September 17, 2010

Difference between system, exec and `` (backticks) in perl

They're all different, and the docs explain how they're different. Backticks capture and return output; system returns an exit status, and exec never returns at all if it's successful.

exec

executes a command and never returns. It's like a return statement in a function.
If the command is not found execute returns false. It never returns true, because if the command is found it never returns at all. There is also no point in returning STDOUT, STDERR or exit status of the command. You can find documentation about it in perlfunc, because it is a function.

system

executes a command and your perl script is continued after the command has finished.
The return value is the exit status of the command. You can find documentation about it in perlfunc.

System fork the current process into two processes (parent and child) and replace the child process
with an exec call to the external program while the parent process  waits for the child process to complete.  This is what the system
function** does.  It is implemented in terms of a fork***, an exec, and a waitpid****. 

backticks

like system executes a command and your perl script is continued after the command has finished.
In contrary to system the return value is STDOUT of the command. qx// is equivalent to backticks. You can find documentation about it in perlop, because unlike system and exec it is an operator.

No comments:

Post a Comment