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

Greetings, esteemed monks! I am trying to write a program which will print a line of input that it reads from a named pipe. As each new line of input appears on the pipe, I want my program to send it to stdout. Not sure if it is relevant, but this is on FreeBSD. Here is the code I am using:
use FileHandle; $tmpfile = "/var/tmp/fifotest"; open (FIFO, "$tmpfile"); STDOUT->autoflush(1); FIFO->autoflush(1); print <FIFO>; close (FIFO);
However, when I write to my named pipe with another program, output will not appear from my perl script above until I send an EOF to the pipe. I ran truss on my perl script, and I see it reading in the data as each line is fed to the pipe, but it doesn't do any writing until the EOF goes to the pipe from the other program which is writing to the pipe. Is this due to some kind of buffering issue? Any way to accomplish this? Thanks!

Replies are listed 'Best First'.
Re: line by line reading named pipes
by jethro (Monsignor) on Feb 05, 2011 at 03:41 UTC

    print provides list context and <> in list context reads the complete data, i.e. until EOF, before doing anything else. Use

    while (<FIFO>) { print }