Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Clobber instead of append

by OverlordQ (Hermit)
on May 26, 2005 at 03:45 UTC ( [id://460498] : perlquestion . print w/replies, xml ) Need Help??

OverlordQ has asked for the wisdom of the Perl Monks concerning the following question:

Greetings monks, I'm trying to strip out some HTML from a list of files, but instead of overwriting my changes it seems they're getting appended to the end. Here's my code:
foreach my $file (@files) { open(FILE,"+<$file"); my @stream = <FILE>; my $stream = join '', @stream; $stream =~ s/(<iframe.*iframe>)//s; print FILE $stream; close(FILE); }
Do I need to read the file in, unlink, open up a write handle and then write it back?

Replies are listed 'Best First'.
Re: Clobber instead of append
by Tanktalus (Canon) on May 26, 2005 at 03:51 UTC

    You need to seek back to the beginning to write to the beginning. You're leaving the stream pointer at the end of the file, so you want to reset it. You may also want to look at truncate because you'll be removing characters, and you'll want to remove excess file.

    It is simpler, however, to write to a new file, delete the original, and rename the new file to the old name. Or, rather than delete the original, rename it to a backup name. Much safer, too.

Re: Clobber instead of append
by Mr_Person (Hermit) on May 26, 2005 at 03:58 UTC
    You can't read and write to the file at the same time like that. You're certainly free to use the same file handle, and that looks like that's what you meant to do with the open modes you chose. Just read in the whole file, then seek to the beginning, write out what you want to, and truncate it to the length of what you've written.


    open(FILE, "+<$file"); my $temp; while(<FILE>) { $temp .= $_; } seek(FILE,0,0); print FILE $temp; truncate(FILE,tell(FILE)); close(FILE);

    Of course, you'll want to add some dies to that.

    If you're worried about memory usage, I'd suggest reading the file in one line at a time, writing out to a temp file one line at a time, then just moving the temp file over the original (don't forget to preserve permissions).

Re: Clobber instead of append
by astroboy (Chaplain) on May 26, 2005 at 04:23 UTC
    You can also read and write to the same file using the IO::InSitu module. An example form the doco seems to suit your requirements (the in and out filenames can be the same)
    use IO::InSitu; my ($in, $out) = open_rw($infile_name, $outfile_name); for my $line (<$in>) { $line =~ s/foo/bar/g; print {$out} $line; }