# trying to optimize performance and memory usage # by replacing foreach() with while() my $file = "./test.txt"; my $tempfile = "./tempfile".(rand()*9999); open (LOG, $file); flock LOG, 1; open (TMP, ">$tempfile"); flock TMP, 2; while (my $line = ){ # here we do some manipulations with $line print TMP $line; } close TMP; rename($tempfile,"$file"); close LOG;