Thanks Kevin for pointing out the Cookbook receipe. I dug out my Perl Cookbook, 2nd edition. The "How to modify a file in place without a temporary file" is still in Chapter 7, but it's now receipe 17.
The discussion for this receipe is very interesting. I'm not sure if it's legal to quote the book, so I'll paraphrase. My read is that this method is discouraged, especially if there's other alternatives. The discussion mentions a few drawbacks to this method, but the main drawback appears to be the potential to eat lots of memory with a large file, though this works fine with small files. >From the the Cookbook, it appears the preferred method is to use a temporary file for this sort of task. (Chapt 7/Receipe 15.) Basically, this method reads the original file, writes changes to a temp file, the renames the temp back to the original. They say the advantages of the method are that it uses little memory (compared to not using a temp file), as well as being easier and safer to program. I learned something here! Thanks again for pointing out the Cookbook receipe. - Stuart Kevin Old <[EMAIL PROTECTED]> 01/25/2004 03:03 PM To [EMAIL PROTECTED] cc [EMAIL PROTECTED] Subject Re: How to put a variable value into a text file On Sun, 2004-01-25 at 14:33, [EMAIL PROTECTED] wrote: > Hi all: > > I'm trying to put a variable value into a text file. I've tried a few > things with no success. > > Anyone know how to do this ? Is it doable ? I'm pretty sure I read how > to do this somewhere, so I'm about to hit the Perl books to see if I can > find it. > Any help will be appreciated. > > Here's what I last tried. It doesn't work and I'm embarrassed to even > show it. But, I did want to show that at least I'm trying to make it > work: > > use strict; > use warnings; > > open (INFILE, "c:\\testmessage.txt") or die "The source file failed to > open - $!"; > my @testarray = <INFILE>; > my $name = "stuart"; > print @testarray; > > Here's what it prints: My name is $name. > > Here's the testmessage.txt file: My name is $name. > > After running my script, I'd like the testmessage.txt to read: My name is > stuart. > > Thanks again for any help. Hi Stuart, >From the Perl Cookbook Chapter 7, Recipe 10 " 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: $!"; The idea is that you read the file into an array (each element in the array is a line), modify the lines in the array you'd like to, then "Seek" the file position back to the beginning and write the array to the file, therefore resulting in a "modified" file. HTH, Kevin -- Kevin Old <[EMAIL PROTECTED]>