This is what Expect is for. The Expect module handles the pseudo-tty details, so you don't have to!

This program is called 'consumer.pl'. I added a clue for Expect to use to find the end of the output. You can do without the ending markers, you just have to be a little more clever with Expect. This sample program is similar to yours, in that it uses multiple input streams. It writes a file for each stream.

use File::Slurp; my $out=''; while(<>) { $out .= "1:$_"; } $out .= "end1\n"; write_file("out1.txt", $out); $out=''; while(<>) { $out .= "2:$_"; } $out .= "end2\n"; write_file("out2.txt", $out);
Here is a program to communicate with it, and send it two streams of data. The Expect module sends control-D with the string "\cD".
use Expect; my $proc= new Expect(); $proc->spawn("./consumer.pl"); $proc->send("hello there\n"); $proc->send("\cD"); $proc->send("this is\n"); $proc->send("\cD"); $proc->expect(1, [ /end1/ => sub { exp_continue; } ], [ /end2/ => sub { exp_continue; } ] );
I'm sure that this code could be better, it is intended as proof of concept. I have only used Expect a few times. I find it difficult but worthwhile.

minor update: pushed the code around a bit

It should work perfectly the first time! - toma

In reply to Re: what is EOF and how can I send it? by toma
in thread what is EOF and how can I send it? by revdiablo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.