in reply to Re: writing to top of a file
in thread writing to top of a file

Hello

You said: The example above is ok, but if the file is very large, you might want to use join the data.

Joining the data will not eleminate the problem, it will only delay it (and not by much for that matter). If the file is very large, use:

use File::Temp qw/tempfile/; open $FIN, "<$file1" or die .... my ($fh, $fname) = tempfile(); print $fh "FIRST LINE\n"; print $fh $_ while <$FIN>; close $FIN; close $fh; rename $fname, $file1;
Code to check success of operations is not included for readability

Aziz,,,

Update: Thanks runrig for the comment. The point is the same, don't read all data into memory when moving data from file to file.

Replies are listed 'Best First'.
Re: Re: Re: writing to top of a file
by runrig (Abbot) on Aug 06, 2001 at 22:46 UTC
    Just use File::Copy (more efficient than reading/writing one line at a time):
    use File::Temp qw/tempfile/; use File::Copy; my ($fh, $fname) = tempfile(); syswrite $fh, "FIRST LINE\n"; copy($file1, $fh) or die "Error copying: $!"; close $fh;
    rename $fname, $file1;