Because FLOSS is handy, isn’t it?

Dynamic file read with Perl

Posted by isilanes on July 6, 2006

Blog moved to: handyfloss.net

Entry available at: http://handyfloss.net/2006.07/dynamic-file-read-with-perl/

GNU/Linux command-line users, programmers and hackers worldwide have probably come to know and love the wonderful tail shell command, together with cat, head, grep, awk and sed, easily one of the single most usefull commands.

A killer feature of tail is the -f (--follow) argument, which outputs the last lines of a file and then keeps waiting for new lines that might keep appearing in the file, and show them on the screen when they do. This is invaluable to keep track of, e.g., logfiles where new entries are being added all the time, and one does not want to be doing a tail by hand.

Since I am a great fan of Perl, and use its scripts for anything short of cooking dinner (but wait…), I have found myself in situations where I had to tail the last lines of a file. This can be done in several ways:

system "tail $file"


my $str = `tail $file`;
print $str;

or with a open() statement, then reading the whole file (or a part), and printing it. The first example with system is the most “direct” one, but reading the file (or a part) into a variable is very handy for doing with it all the nifty things Perl does so well to text strings (substituting, deleting, including, reordering, comparing…).

However, when tail -f was needed (i.e., keep on tracking the file and operate on the output as it appears), I kept using system calls, and all the formatting had to be done in the shell spawned by the system call, not by Perl. This was sad.

So, I was so happy when I discovered a simple trick to make open() read dynamically. There are better ways of doing it, more efficiently and correctly, but this one works, and is quite simple. If efficience is vital for you, this is not probably the place to learn about it. Actually, if you look for efficiency, you shouldn’t be using Perl at all :^)

Example of Perl code that reads dynamically a file “$in” (the quotes in ‘<‘ are there to prevent incorrect display in this f*cking blog):

open(INFILE,"tail -0f $in |") || die "Failed!\n";
while(my $line = <INFILE>)
  do whatever to $line
  print "$line\n";

4 Responses to “Dynamic file read with Perl”

  1. MS said

    Is this syntax correct? (I was thinking about the “my $line = `” command.)

  2. isilanes said

    Thanks for pointing out, MS. Indeed, it’s not correct. There is some text missing. Blame it on the horrible code printing capabilities of WordPress, and above it to my horrible incompetence on checking. I’ll fix it.

    However, notice that my blog moved to http://handyfloss.net. You can read this post there at: http://handyfloss.net/2006.07/dynamic-file-read-with-perl/

  3. Carlos said

    So what is the proper syntax then? Sorry I’m new to perl so I don’t really know what the while(my $line = ) line does.

  4. isilanes said

    No problem, Carlos. Please refer to http://handyfloss.net/2006.07/dynamic-file-read-with-perl/ to find an updated version of this post, wherein the last code portion is explained.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: