http://www.perlmonks.org?node_id=105801


in reply to Read from a file and replace

What you want is called "inplace editing" in perl. Here it is, try to spot the difference to your own code:
$^I = ".bak"; @ARGV = ('file.html'); while(<>) { # chomp; # don't want to loose the linebreaks my $string = "$_"; my $find = "www.3sat.de/boerse/boerse_service.html"; my $replace = "there.company.com/"; $find = quotemeta $find; $string =~ s/$find/$replace/g; print $string; }

Here's what you do:

  1. Put the list of files you want to treat into @ARGV
  2. Set $^I. For every file treated, the original is saved with $^I added to the filename
  3. read from <>
  4. print to STDOUT

Perl does all the right things: it opens the files in @ARGV one by one, creates backups, and writes your output back into the original files.

P.S. Everything has already been discussed on perlmonks: see modifying and overwriting a file

--
Brigitte    'I never met a chocolate I didnt like'    Jellinek
http://www.horus.com/~bjelli/         http://perlwelt.horus.at