Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks:
I need your help.
I need to redirect the line by line output from system($command) to an array instead of an external output file. Normally redirecting to an output file, one would do this:
my $command="/usr/local/coms/command >comm.out"; system($command);
where comm.out contains the redirected output from command.
Now I thought that perhaps doing this:
my $command="/usr/local/coms/command "; system($command)>@array;
would simple redirect the output to the array @array
Unfortunately.... this didn't work. How do I accomplish this? Any help/insight as to how this can be done, as always, is greatly appreciated. Thank you in advance for your help!

Replies are listed 'Best First'.
Re: Redirecting OUTPUT of system($command) to an array
by Zaxo (Archbishop) on May 13, 2004 at 18:32 UTC

    You can read the output of the command from a filehandle with piped open,

    my $pid = open my $sys, '-|', '/usr/local/coms/command' or die $!; my @array = <$sys>; close $sys;

    After Compline,
    Zaxo

      Ok --- I believe this is headed on the right track but... I get the following error: (line 13) is the first line of code that you suggested in your original response:
      $ ./linkSNR2.pl Can't exec "spawn /bin/bash ": No such file or directory at ./script.pl line 13. No such file or directory at ./script.pl line 13. $
      my line 13:
      my $pid = open my $sys, "-|", `$command` or die $!;
      The OS I am running on is Sun Solaris 8.
      ANy additional thoughts on this matter? Their must be some sublty that I am missing.
      Thanks Again for your help!

      Note: Actual filenames and command references are changed in accordance with security protocols

        You have $command in single quotes, which do not interpolate the variable's value. You're trying to execute literal '$command'.

        Update: Oops, no, that's backticks. Worse yet. You're trying to execute a file named by the text output by $command.

        After Compline,
        Zaxo

Re: Redirecting OUTPUT of system($command) to an array
by Nkuvu (Priest) on May 13, 2004 at 18:21 UTC
    my $command="/usr/local/coms/command "; my @array = `$command`;

    Backticks are what you're looking for.

      Nkuvu,
      I would guess that this is 1 step short of being what is desired ultimately. The output likely needs to be split (by either horizontal or vertical whitespace).
      my $command = "/usr/local/some/command"; my $result = `$command`; my @array = split /\n/ , $result;
      Otherwise the entire result ends up in slot 0 of the array you will have no control over how the output is stored into the array.

      Cheers - L~R

      Update: If the desired outcome is to split on newlines, then there is no need to store the result into a temporary scalar and split yourself as japhy points out below.
        Except that readpipe() (which is what implements `...` and qx()) returns a list of lines in list context. So an array on the left-hand side is fine.
        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Thanks for the caveat, I'll keep that in mind. So far, every time I've resorted to using backticks, the system command I've been executing has output multiple lines of information.