in reply to slighly newbish question

I think your problem is with the other program but without more information it is hard to tell.

If the other program is not flushing its buffer correctly with every new element then you may be getting strange side effects.

$|=1; switches off any write buffering in perl.

Also without locking the file if the other program appends a line while you are trying to truncate down to 20 lines then that update will be lost.

Hope this helps
UnderMine