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

Dear Monks

I have been lately wondering, if I could pass the output generated by an external binary file or executable file, which has been invoked by system command and that generates some output. What usually I do is, I write the output in a file and then read the output via normal file handling and parse the output or what ever I intend to do.

########################### ## ## $input contains the necessary parameters for the external executab +le file and the output is generated into output_file created ## ########################### `some_external_excutable_file $input >output_file` open (OUTPUT,"output_file"); @output=<OUTPUT>; close OUTPUT; ############################ ## ## I read the output file into an array and use the array for my furt +her analysis ## ############################

But the spurring question here is, isn't there any other way to pass the output generated by the external executable directly into an array rather than the way I did. Is there any default variable for accomplishing it?

Replies are listed 'Best First'.
Re: How to pass the output into an array
by cdarke (Prior) on May 17, 2012 at 07:07 UTC
    `Back-ticks`, or qx, redirects the external program's stdout stream back into perl. Used in list context you get one element for each line of output:
    my @output = `some_external_excutable_file $input`;
    Of course you should be careful that there is not so much output that it busts memory.
Re: How to pass the output into an array
by kcott (Archbishop) on May 17, 2012 at 07:15 UTC

    My first thought was to just pipe the output from your system command to your perl code, something like:

    some_external_excutable_file $input | perl_prog.pl

    You can also run the system command from within your perl code. Take a look at open. About a third of the way down, examples like

    open(ARTICLE, "-|", "caesar <$article") ... open(ARTICLE, "caesar <$article |") ...

    are probably close to the type of code you want.

    I'm not sure what you mean by "Is there any default variable for accomplishing it?". I may have missed your meaning: all the default variables are documented in perlvar, if that's of any help.

    -- Ken