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

I need to run a program on the os and then loop through that data and submit it to another os program. I am able to execute the first program but am unclear as to how to use the data that it produces. Any ideas?

Replies are listed 'Best First'.
Re: using data from an os program
by toolic (Bishop) on Jan 08, 2016 at 14:56 UTC
    Use qx if you want to capture the output of a program.
Re: using data from an os program
by choroba (Cardinal) on Jan 08, 2016 at 15:27 UTC
    toolic's advice is good. If your program produces lots of data, or takes a long time to complete, but you can process its output line by line before it finishes, you can use open:
    open my $PIPE, '-|', 'my-first-program', 'arg1', 'arg2' or die $!; while (my $line = <$PIPE>) { chomp; # ... do something with the line } close $PIPE or die $!;

    You can output the processed data to the second program in a similar way:

    open my $PIPE_OUT, '|-', 'my-second-program', @args or die $!; # ... print {$PIPE_OUT} $data;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: using data from an os program
by kcott (Archbishop) on Jan 09, 2016 at 09:16 UTC

    G'day sewood,

    Welcome to the Monastery.

    If you're doing this on the command line, you can just pipe STDOUT from your first program to your Perl program and, in turn, pipe its STDOUT to your second program. Something like this:

    $ os_prog_1 | perl_prog | os_prog_2

    See "perlop: I/O Operators" for full details of reading STDIN in your Perl code. Here's a highly contrived, minimal example which just reads input and passes it on unchanged (presumably, you'll want some processing to actually occur):

    $ ls -l | perl -e 'while (<>) { print }' | wc -l 821

    And, just to confirm the Perl code was passing on each line:

    $ ls -l | wc -l 821

    — Ken