Modifying a File in Place Without a Temporary File (Perl Cookbook, 2nd Edition)

## 7.17. Modifying a File in Place Without a Temporary File

### 7.17.1. Problem

You need to insert, delete, or change one or more lines in a file, and you don't want to (or can't) use a temporary file.

### 7.17.2. Solution

Open the file in update mode ("`+<`"), read the whole file into an array of lines, change the array, then rewrite the file and truncate it to its current seek pointer.

>     open(FH, "+<", $FILE)               or die "Opening: $!";
>     @ARRAY = <FH>;
>     # change ARRAY here
>     seek(FH,0,0)                        or die "Seeking: $!";
>     print FH @ARRAY                     or die "Printing: $!";
>     truncate(FH,tell(FH))               or die "Truncating: $!";
>     close(FH)                           or die "Closing: $!";

### 7.17.3. Discussion

As explained in this chapter's Introduction, the operating system treats files as unstructured streams of bytes. This makes it impossible to insert, modify, or change bits of the file in place. (Except for the special case of fixed-record-length files, discussed in Recipe 8.13.) You can use a temporary file to hold the changed output, or you can read the entire file into memory, change it, and write it back out again.

Reading everything into memory is fine for small files, but doesn't scale well. Trying it on your 800 MB web server log files will either deplete your virtual memory or thrash your machine's VM system. For small files, though, this works:

>     open(F, "+<", $infile)      or die "can't read $infile: $!";
>     $out = "";
>     while (<F>) {
>         s/DATE/localtime/eg;
>         $out .= $_;
>     }
>     seek(F, 0, 0)               or die "can't seek to start of $infile: $!";
>     print F $out                or die "can't print to $infile: $!";
>     truncate(F, tell(F))        or die "can't truncate $infile: $!";
>     close(F)                    or die "can't close $infile: $!";

For other examples of things you can do in-place, look at the recipes in Chapter 8.

This approach is only for the truly determined. It's harder to write, takes more memory (potentially a lot more), doesn't keep a backup file, and may confuse other processes trying to read from the file you're updating. For most purposes, therefore, we suggest it's probably not worth it.

Remember to lock if you're paranoid, careful, or both.

### 7.17.4. See Also

The `seek`, `truncate`, `open`, and `sysopen` functions in perlfunc(1) and in Chapter 29 of Programming Perl; Recipe 7.15; Recipe 7.16; Recipe 7.18

Copyright © 2003 O'Reilly & Associates. All rights reserved.