in reply to Redirecting OUTPUT of system($command) to an array

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

Replies are listed 'Best First'.
Re: Re: Redirecting OUTPUT of system($command) to an array
by Anonymous Monk on May 13, 2004 at 19:04 UTC
    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

        I just implemented this line:
        my $pid = open my $sys, "-|", $command or die $!;
        And.... It work Gloriously!!!!
        Thanks for all your help!!!
        system("ls > holder.log"); #send the directory listing to a holder file open( TMPFILE, "holder.log" ) || die "Failed to create log file: $!\n"; @dirlist = <TMPFILE>; #read in the directory from the file to the array $fh = join(" ", @dirlist); #convert the array into a scalar for pattern matching close( TMPFILE ); #tidy up system("rm holder.log");