in reply to Deleting last line of file

Strange that nobody has mentioned truncate. This would get rid of the overhead of rewriting the whole file.
#!/usr/bin/perl # # given a file name as an argument, # read the file until eof, back up # one line and truncate the file there. # in short, "delete the last line of a file" # my $file=shift(@ARGV); open(FILE,"+<$file") or die; while (<FILE>) { if (eof(FILE)) { seek(FILE,-(length($_)),2) or die; truncate(FILE,tell(FILE)) or die; } } close FILE;
This would be faster for large files if it would seek() to eof, then read backwards looking for the newline. Seemed a bit cumbersome for an example though.