Reading command output to a Perl array

I want to get the output of a command into an array - for example:

my @output = `$cmd`; 

but it seems that the output of the command is not in the @output array.

Any idea where he is going?

+8
command perl
source share
3 answers

This simple script works for me:

 #!/usr/bin/env perl use strict; use warnings; my $cmd = "ls"; my @output = `$cmd`; chomp @output; foreach my $line (@output) { print "<<$line>>\n"; } 

He made a conclusion (with the exception of triple points):

 $ perl xx.pl <<args>> <<args.c>> <<args.dSYM>> <<atob.c>> <<bp.pl>> ... <<schwartz.pl>> <<timer.c>> <<timer.h>> <<utf8reader.c>> <<xx.pl>> $ 

The output of the command is divided into line boundaries (by default in the context of the list). chomp removes newlines in array elements.

+10
source share

The output (standard) goes to this array:

 david@cyberman:listing # cat > demo.pl #!/usr/bin/perl use strict; use warnings; use v5.14; use Data::Dump qw/ddx/; my @output = `ls -lh`; ddx \@output; david@cyberman:listing # touch abcd david@cyberman:listing # perl demo.pl # demo.pl:8: [ # "total 8\n", # "-rw-r--r-- 1 david staff 0B 5 Jun 12:15 a\n", # "-rw-r--r-- 1 david staff 0B 5 Jun 12:15 b\n", # "-rw-r--r-- 1 david staff 0B 5 Jun 12:15 c\n", # "-rw-r--r-- 1 david staff 0B 5 Jun 12:15 d\n", # "-rw-r--r-- 1 david staff 115B 5 Jun 12:15 demo.pl\n", # ] 
+6
source share

Enable automatic error checking:

 require IPC::System::Simple; use autodie qw(:all); โ‹ฎ my @output = `$cmd`; 
+2
source share

All Articles