in reply to writing to the top of a file

Unless reads from this file are considerably more frequent than writes, reading the whole file and then re-writing it with the new stuff at the top in order to speed up reads will have dubious benefit.

If the reads and writes are paired (equal numbers of each) then one cycle of 'Writing to the end of file + reading the whole file to get the last line' compares favourably with 'Reading the whole file, writing the new line and then re-writing the whole file + reading the first line'.

You would almost certainly be better of writing new stuff to the end, and then using File::ReadBackwards or a similar technique to retrieve it.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: writing to the top of a file
by bibo (Pilgrim) on May 20, 2004 at 13:37 UTC
    I concur. Even sucking the whole file in with @lines = <$INFILE>; and just printing the results in reverse order makes more sense than physically writing to the file in such an unnatural order. Of course, that presumes the file is not of a monstrous size, etc etc.