title: Perl bidirectional pipe IPC, how to avoid output buffering
body: <p>I am trying to communicate with  an interactive process.  I want my perl script to be a "moddle man" between the user and the process.  The process puts text to stdout, prompts the user for a command, puts more text to stdout, prompts the user for a command, .......  A primitive graphic is provided:</p>
  
  <pre><code> User &lt;----STDOUT---- interface.pl &lt;-----STDOUT--- Process
   User -----STDIN----&gt; interface.pl ------STDIN---&gt; Process
   User &lt;----STDOUT---- interface.pl &lt;-----STDOUT--- Process
   User -----STDIN----&gt; interface.pl ------STDIN---&gt; Process
   User &lt;----STDOUT---- interface.pl &lt;-----STDOUT--- Process
   User -----STDIN----&gt; interface.pl ------STDIN---&gt; Process
  </code></pre>
  
  <p>The following simulates what I'm trying to do:</p>
  
  <pre><code>    #!/usr/bin/perl
  
      use strict;
      use warnings;
  
      use FileHandle;
      use IPC::Open2;
      my  $pid = open2( \*READER, \*WRITER, "cat -n" );
      WRITER-&gt;autoflush(); # default here, actually
      my $got = "";
      my $input = " ";
  
      while ($input ne "") {
              chomp($input = &lt;STDIN&gt;);
              print WRITER "$input \n";
              $got = &lt;READER&gt;;
              print $got;
      }
  </code></pre>
  
  <p>DUe to output buffering the above example does not work.  No matter what text is typed in, or how many enters are pressed the program just sits there.  The way to fix it is to issue:</p>
  
  <pre><code>    my  $pid = open2( \*READER, \*WRITER, "cat -un" );
  </code></pre>
  
  <p>Notice "cat -un" as opposed to just "cat -n".  -u turns off output buffering on cat.  When output buffering is turned off this works.  The process I am trying to interact with most likely buffers output as I am facing the same issues with "cat -n".  Unfortunately I can not turn off output buffering on the process I am communicating with, so how do I handle this issue?</p>
  
  <p>UPDATE1 (using ptty):</p>
  
  <pre><code>    #!/usr/bin/perl
  
      use strict;
      use warnings;
  
      use IO::Pty;
      use IPC::Open2;
  
      my $reader = new IO::Pty;
      my $writer = new IO::Pty;
  
      my  $pid = open2( $reader, $writer, "cat -n" );
      my $got = "";
      my $input = " ";
  
      $writer-&gt;autoflush(1);
  
      while ($input ne "") {
              chomp($input = &lt;STDIN&gt;);
              $writer-&gt;print("$input \n");
              $got = $reader-&gt;getline;
              print $got;
      }
  </code></pre>
  
  <p>~                    </p>