http://www.perlmonks.org?node_id=488610


in reply to printing to filehandles

You have to flush your file handle after printing to write to the disk immediately, otherwise it gets conveniently buffered in memory.
use FileHandle; ... open OUT, ...; autoflush OUT 1; print OUT ...;

The alternative is to close the file after you have finished reading, and open it again next time before writing to it. Keep the time that the file is openned as short as possible.