Reading output from command into Perl array

前端 未结 3 1090
无人及你
无人及你 2020-12-20 13:26

I want to get the output of a command into an array — like this:

my @output = `$cmd`;

but it seems that the output from the command

3条回答
  •  旧时难觅i
    2020-12-20 13:52

    The (standard) output does go to that 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 a b c d
    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",
    # ]
    

提交回复
热议问题