title: Perl STDIN without buffering or line buffering
body: <p>I have a Perl script that received input piped from another program. It's buffering with an 8k (Ubuntu default) input buffer, which is causing problems. I'd like to use line buffering or disable buffering completely. It doesn't look like there's a good way to do this. Any suggestions?</p>
  
  <pre><code>use IO::Handle;
  use IO::Poll qw[ POLLIN POLLHUP POLLERR ];
  use Text::CSV;
  
  my $stdin = new IO::Handle;
  $stdin-&gt;fdopen(fileno(STDIN), 'r');
  $stdin-&gt;setbuf(undef);
  
  my $poll = IO::Poll-&gt;new() or die "cannot create IO::Poll object";
  $poll-&gt;mask($stdin =&gt; POLLIN);
  
  STDIN-&gt;blocking(0);
  
  my $halt = 0;
  for(;;) {
      $poll-&gt;poll($config{poll_timout}); 
  
      for my $handle ($poll-&gt;handles(POLLIN | POLLHUP | POLLERR)) {
          next unless($handle eq $stdin);
  
          if(eof) {
              $halt = 1;
              last;
          }
  
          my @row = $csv-&gt;getline($stdin);
          # Do more stuff here
      }
  
      last if($halt);
  }
  </code></pre>
  
  <p>Polling STDIN kind of throws a wrench into things since IO::Poll uses buffering and direct calls like sysread do not (and they can't mix). I don't want to infinitely call sysread without no blocking. I require the use of <code>select</code> or <code>poll</code> since I don't want to hammer the CPU.</p>
  
  <p><strong>PLEASE NOTE: I'm talking about STDIN, NOT STDOUT. $|++ is not the solution.</strong></p>
  
  <p><strong>[EDIT]</strong>
  Updating my question to clarify based on the comments and other answers.</p>
  
  <p>The program that is writing to STDOUT (on the other side of the pipe) is line buffered and flushed after every write. Every write contains a newline, so in effect, buffering is not an issue for STDOUT of the first program.</p>
  
  <p>To verify this is true, I wrote a small C program that reads piped input from the same program with STDIN buffering disabled (setvbuf with _IONBF). The input appears in STDIN of the test program immediately. Sadly, it does not appear to be an issue with the output from the first program.
  <strong>[/EDIT]</strong></p>
  
  <p>Thanks for any insight!</p>
  
  <p>PS. I have done a fair amount of Googling. <a href="https://stackoverflow.com/questions/12351500/perl-disable-buffering-input">This link</a> is the closest I've found to an answer, but it certainly doesn't satisfy all my needs.</p>