As many has pointed out IO::Pty is the right module to use. The buffer comes from the external program, not the IPC::Open3. Posted below is the complete program to demonstrate how this is done with IO::Pty.

#!/usr/bin/env perl -w use strict; use IO::Pty; my $master = new IO::Pty; my $slave = $master->slave(); my $pid = fork(); die "Couldn't fork: $!" unless defined $pid; if ($pid) { $master->close_slave(); while ( !$master->eof ) { my $line = $master->getline; print "TIME: " . localtime() . " OUTPUT: " . $line; if ( $line =~ m/^Please press return/ ) { $master->print("abc\n"); } } wait(); exit $? >> 8; } else { $master->close(); $master->make_slave_controlling_terminal(); open( STDIN, ">&", $slave ) or die "Couldn't dup stdin: $!"; open( STDOUT, ">&", $slave ) or die "Couldn't dup stdout: $!"; open( STDERR, ">&", $slave ) or die "Couldn't dup stderr: $!"; # External program with buffered IO. system q( perl -e ' print "Please press return\n"; my $line = <STDIN>; print "OK, $line, exit now\n"; exit 14; ' ); }

If you run the program, you will get this:

TIME: Thu Jul  3 19:30:47 2014 OUTPUT: Please press return
TIME: Thu Jul  3 19:30:47 2014 OUTPUT: abc
TIME: Thu Jul  3 19:30:47 2014 OUTPUT: OK, abc
TIME: Thu Jul  3 19:30:47 2014 OUTPUT: , exit now

The second line comes from echo to the $master, how do I turn off echo?


In reply to Re: IPC::Open3 buffering and autoflush by puravida
in thread IPC::Open3 buffering and autoflush by morpheus

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.