I suggest to stronger interleave reading and writing of
whole (processed) records or lines keeping $tempContent short. Try to calculate the CRC incrementally. E.g.
String::CRC32 or
pack could be helpful here. The last chunk to contribute to the CRC will be the trailing "\n".