Shawn H Corey wrote:
On 11-04-05 09:24 AM, ANJAN PURKAYASTHA wrote:
Hi,
Is there any way to run a shell command from within a perl script and
capture the output in, say, an array?
One can run a shell command through the system function. But system
itself
just returns a status code.


You could use open:

#!/usr/bin/env perl

use strict;
use warnings;

open my $ls_fh, '-|', 'ls' or die "could not open `ls`: $!\n";
chomp( my @files = <$ls_fh> );
close $ls_fh or die "could not open `ls`: $!\n";

Read:

perldoc -f close

To see the proper way to close a pipeed open.



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
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