On Tue, Jun 17, 2008 at 01:47, swaroop <[EMAIL PROTECTED]> wrote:
> As we know there are 3 ways a system shell command to be executed.
>
> 1.> $var = system("command");
> 2.> $var = exec("command");
> 3.> $var = `command`;
>
> What is difference between these three?  I found that the first two
> options are printing the output of the command.  And the third one
> simply stores the output in the variable.

The exec function* replaces the current process with the one specified
in the arguments, so exec("bash", "-l") would replace the perl process
with bash login shell running interactively.  This is most often not
what you want to do.  You want to run an external program and wait for
it to finish.  To do this you would need to 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****.  Due to the way fork works the file handles of the
parent are inherited by the child, so STDOUT and STDERR in the process
created by system are the same as in the Perl program.  This is why
output shows up when you use system or exec.

The qx// operator***** (also known as ``) is very different.  It is
most likely implemented via the popen****** function from
SUSv2*******.  It captures the STDOUT (but not the STDERR) of an
external program and returns it as a list of lines (in list context)
or as string (in scalar context).

Other options for running (and capturing output/providing input)
include the open function******** (STDOUT or STDIN only),
IPC::Open2********* (STDIN and STDOUT), and IPC::Open3**********
(STDIN, STDOUT, and STDERR).

If you are not writing a quick and dirty program the open function is
preferable to the qx// operator because you have more control and the
output data shows up as soon as possible (rather than at the end of
the external program's run):

open my $output, "-|", "program_to_execute", "argument 1", "argument 2"
    or die "could not run program: $!";

while (my $line = <$output>) {
    #do something with a line printed by program_to_execute
    #$line shows up here as soon as it is flushed
}

IPC::Open2 and IPC::Open3 provide even more control, and are useful
for puppet stringing other programs.

* http://perldoc.perl.org/functions/exec.html
** http://perldoc.perl.org/functions/system.html
*** http://perldoc.perl.org/functions/fork.html
**** http://perldoc.perl.org/functions/waitpid.html
***** http://perldoc.perl.org/perlop.html#qx/STRING/
****** http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html
******* Single UNIX Specification, the thing that replaced POSIX, see
http://en.wikipedia.org/wiki/Single_UNIX_Specification
******** http://perldoc.perl.org/functions/open.html
********* http://perldoc.perl.org/IPC/Open2.html
********** http://perldoc.perl.org/IPC/Open3.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to