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

I am using perl5 on linux OS. I am working on a perl program which will read a flat file and extract some contents from it which matches a particular pattern. The output is given to STDOUT. I want to use this ouput from STDOUT as STDIN for another program without using any intermediate files. How is it possible to achieve this either by using pipes or filters(UNIX)? Or is there any other approach? Please give your suggestion as soon as possible as I need it urgently. Thanks in advance. Manoj

Replies are listed 'Best First'.
Re: Passing ouput between program
by tachyon (Chancellor) on Jul 16, 2001 at 11:05 UTC
    $ ls | more $ perl myscript1 | myscript2

    You can do this using a pipe |. In the first example the output of the ls command is piped to more so you get the results one screenful at a time. In the scond the output of myscript1 is piped to myscript2 which will use it as input. myscript2 of course needs to be expecting the input from myscript1 to appear in STDIN for this to work.

    Hope this helps.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks a lot for the valuable info. It helped me.

      Manoj

      Edit ar0n 2001-07-17

Re: Passing ouput between program
by wine (Scribe) on Jul 16, 2001 at 14:04 UTC
    You're kinda vague on what the problem is, using pipes or creating a perl-program that uses STDIN as input. So perhaps you'll find my comment utterly redundant.

    STDIN can be read line-by-line with:

    while(<>){}

    Each line is assigned to $_. Now you can print a specific line to STDOUT if it matches a centain $expression:

    print if /$expression/;

    Or only print the part of that line if it matches:

    print $1 if /($expression)/;

    Everything together

    while(<>){print if /$expression/;}

    You could also use a commandline thingy like:

    perl -ne 'print if /myword/;'

    Where -n tells Perl to automatically wrap a while(<>){} around your code.

    Look at tachyons comment for pipes

Re: Passing ouput between program
by synapse0 (Pilgrim) on Jul 16, 2001 at 16:07 UTC
    Well, asking the sage advice of the One True Perl Guru, Guru perldoc (perldoc -f open from the cmd line), we find this:
    If MODE is `'|-'', the filename is interpreted as a command to which output is to be piped, and if MODE is `'-|'', the filename is interpreted as a command which pipes output to us. In the 2-arguments (and 1-argument) form one should replace dash (`'-'') with the command. See the Using open() for IPC entry in the perlipc manpage for more examples of this. (You are not allowed to `open' to a command that pipes both in and out, but see the IPC::Open2 manpage, the IPC::Open3 manpage, and the Bidirectional Communication entry in the perlipc manpage for alternatives.)
    and this example
    open(ARTICLE, '-|', "caesar <$article") # decrypt article + or die "Can't start caesar: $!";
    Which could just as easily be:
    open(FH, '-|', "myscript.pl") or die "He's dead, Jim :$!"; while(<FH>) { do whatever with output form myscript } close(FH);
    So, you can snarf info from one scripts output to your script using this method, or one of the IPC::OpenX methods.
    Hopefully, that'll give some help.
    -Syn0
Re: Passing ouput between program
by perigeeV (Hermit) on Jul 16, 2001 at 16:00 UTC
    One way is to use open with a pipe.

    open(TWO, "| /your/second/program") or die "Dead $1\n";

    Now instead of printing to STDOUT, print to the filehandle you just made:

    print TWO "data data data\n";

    In your second program that data is read in as STDIN:

    print while(<>);
    will print out "data data data".