Tip

Perl file maintenance

Modifying a file in place without a temporary file

From Perl Cookbook by Tom Christiansen and Nathan Torkington, O'Reilly and Associates, 1998.

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.

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 = ;
 	#  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: $!";
 

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. 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 works for small files, but it doesn't scale well. Trying to 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, it works:


 	open (F, "+< $infile")	           or die "can't read $infile: $!";
 	$out = '';
 	while () {
 		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 more information on the book Perl Cookbook go to http://www.oreilly.com/catalog/cookbook/


    Requires Free Membership to View

This was first published in March 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.