Re: Capturing the output of a shell command

2011-04-05 Thread John W. Krahn
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

Re: Capturing the output of a shell command

2011-04-05 Thread Jim Gibson
On 4/5/11 Tue Apr 5, 2011 7:11 AM, "Chap Harrison" scribbled: > On Apr 5, 2011, at 8:49 AM, Shawn H Corey wrote: > >> 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 = <

Re: Capturing the output of a shell command

2011-04-05 Thread Shawn H Corey
On 11-04-05 10:11 AM, Chap Harrison wrote: Is there any reason not to just use qx? If you use open, any error is in $! which gives you somewhere to start looking if something goes wrong. If you use qx() or back-ticks, any error is in $? which is the child error. This is not an error message

Re: Capturing the output of a shell command

2011-04-05 Thread Rob Dixon
On 05/04/2011 15:11, Chap Harrison wrote: Is there any reason not to just use qx? #!/usr/bin/env perl use strict; use warnings; chomp(my @files = qx(ls)); print "@files\n"; Hi Chap Just being picky, this will print all of the output lines on a single line separated by spaces. Since you pro

Re: Capturing the output of a shell command

2011-04-05 Thread Rob Dixon
On 05/04/2011 14:24, ANJAN PURKAYASTHA wrote: 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 should read the section on qx/STR

Re: Capturing the output of a shell command

2011-04-05 Thread Chap Harrison
On Apr 5, 2011, at 8:49 AM, Shawn H Corey wrote: > 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"; > > print "@file

Re: Capturing the output of a shell command

2011-04-05 Thread ANJAN PURKAYASTHA
thank you all! Anjan On Tue, Apr 5, 2011 at 9:49 AM, 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

Re: Capturing the output of a shell command

2011-04-05 Thread Shawn H Corey
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/

RE: Capturing the output of a shell command

2011-04-05 Thread Sunita Rani Pradhan
Hi Anjan You can execute shell command inside "``" and capture the output in variable . e,g: -- $, = " "; $ls = `ls`; print $ls; @ls = split(/\n/,$ls); print @ls; -- Thanks Sunita -Original Message- From: ANJAN PURKAYASTHA [mailto:anjan.purkayas...@gmail.

Re: Capturing the output of a shell command

2011-04-05 Thread Jins Thomas
On Tue, Apr 5, 2011 at 6:54 PM, ANJAN PURKAYASTHA < anjan.purkayas...@gmail.com> 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