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

Dear Masters,
I have an executable code, which I usually called like this:
$ ./myexecutable.out -O option1 -W option2 -T option3
The result of that code is printed through STDOUT on the screen. Say it looks like this (there are thousands of lines):
MARK : foo MARK : bar
How can I write a Perl script that can capture those result into an array:
my @captured = [ 'MARK: foo', 'MARK: bar', ];
I know Perl has "system" and "exec" function. But I honestly don't know how to use them in this context.
Can anybody give a pointer how can I go about this? Thanks so much beforehand.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Howto pass the STDOUT output of an executable into an array
by Corion (Patriarch) on Feb 13, 2006 at 11:01 UTC

    You want the backtick operator:

    my @output = `./myexecutable.out ...`; chomp @output; # if you don't want the newlines at the end

    See perlop for more information.

Re: Howto pass the STDOUT output of an executable into an array
by tirwhan (Abbot) on Feb 13, 2006 at 11:06 UTC

    I think you have an error in your specification. The last bit of code you give specifies a single array reference as the first value of an array. You probably want one line per array value?

    Anyway, you can use backticks for this

    my @captured = `./myexecutable.out -O option1 -W option2 -T option3`;
    or, if you want the output in an array reference
    my $captured = [(`./myexecutable.out -O option1 -W option2 -T option3` +)];

    All dogma is stupid.
Re: Howto pass the STDOUT of an executable into an array
by jeanluca (Deacon) on Feb 13, 2006 at 12:45 UTC
    What about:
    open (IN,"./myexecutab |") ; @catured = <IN> ;
    or if you would like to make progress visible too:
    open (IN,"./myexecutab |") ; select STDOUT ; $| = 1 ; while(<IN>) { print "." ; ..... }
    Luca