On Tue, Dec 23, 2008 at 13:12, Steve Pittman <spitt...@jhmi.edu> wrote:
> Does any one have a good example?
snip

That depends on what you want to do.  There are five common ways of
executing external programs (including shell scripts):

1. the system function*
2. the qx// operator**
3. the open function***
4. the open2 function from IPC::Open2****
5. the open3 function from IPC::Open3*****

Use system when you just want to execute the program and check it's
return status:

unless (system("ssh", "u...@machine", "touch", "done") == 0) {
    #an error occured so, deal with the return in $?
    #the value is OS dependent, so read up on what your OS does in perlport
}

Use qx// when you want to easily capture the STDOUT from an external program:

my @files = qx{/usr/bin/ssh u...@machine ls};

Use open when you want to capture the STDOUT from an external program
and either need to operate on it as it is written, or if it will be
very large and you don't want to hold the whole output in memory:

open my $fh, "-|", "/usr/bin/ssh", "u...@machine", "find", "/"
    or die "could not execute command: $!";

while (my $line = <$fh>) {
    #do something with each line returned
}

Use the open2 function from IPC::Open2 when you need to write to a
program's STDIN and read from it's STDOUT.

Use open3 from IPC::Open3 when you need to write to a program's STDIN
and read from it's STDOUT and STDERR.

* http://perldoc.perl.org/functions/system.html
** http://perldoc.perl.org/perlop.html#qx/STRING/
*** 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: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to