tail -f in perl

I have been trying to pipe a tail -f feed to a perl program to do some simple filtering on the output using regexes.

I have found that the output of 'tail -f' doesn't show up on STDIN when piped to a different command. Is there a way to do this without actually calling tail -f from within the perl application?

As I write this I found myself wondering if redirecting it to a named pipe could work. I will have to experiment with that.


Are you looking for a "tail -f" module? You have plenty of them on CPAN:


You are probably running into the difference between line and block buffering. Most UNIX utilities use line buffering when in interactive mode. This means characters sent to STDOUT won't appear until you send a newline. Many utilities will switch to block buffering when they don't detect a controlling terminal. In that case no output will be flushed until a certain number of bytes have been sent to STDOUT (often this is 4k). From a program point of view, the easiest thing to do is use File::Tail instead of the UNIX tail command, but when you are writing a quick one off commandline script this is more work than you want to do. I have found that tail piped into just perl works fine. For instance, I often tail logs like this

tail -f log | perl -nle 'print $1 if /attribute="(.*)"/'

About this Entry

This page contains a single entry by leonard published on August 20, 2010 10:29 AM.

Perl golf wrap up. was the previous entry in this blog.

Frozen Perl 2011 Meeting! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.