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

Hi, I know there are on-liners , but would like to understand how to Write to filehandle without deleting all current text.But this doesn't work! It makes the file "abc" empty! What is wrong with my code?
#!/usr/bin/perl $fileName = "abc"; open(IN, "<", "$fileName") or die "Could not open file $fileName"; open(OUT, ">", "$fileName") or die "Could not open file $fileName"; while ($line =(<IN>)) { $new=$line; $new =~ s/foo/bar/g; } print OUT if $new =~ s/foo/bar/g;

Replies are listed 'Best First'.
Re: Write to filehandle without deleting current text
by Corion (Patriarch) on Oct 05, 2010 at 08:58 UTC

    Do you want to append to your output file? See open.

    If you want to change your input file, you shouldn't read and write to the same file at the same time. Write to a different file and rename it after you've made all your changes.

    As a third alternative, have you looked at Tie::File? It allows you to treat a file as an array.

Re: Write to filehandle without deleting current text
by Marshall (Canon) on Oct 05, 2010 at 09:15 UTC
    Perhaps...
    #!/usr/bin/perl -w use strict; my $fileName = "abc"; open(IN, '<', "$fileName") or die "Could not open file $fileName\n"; open(OUT, '>', "$fileName.tmp") or die "Could not open file $fileName.tmp\n"; while (<IN>) { s/foo/bar/g; print OUT $_; } close IN; close OUT; unlink $fileName; rename "$fileName.tmp", $fileName;
      Thank You all! Exactly what I was looking for :-)
Re: Write to filehandle without deleting current text
by suhailck (Friar) on Oct 05, 2010 at 09:01 UTC
Re: Write to filehandle without deleting current text
by ikegami (Patriarch) on Oct 05, 2010 at 16:18 UTC
    { local $^I = ''; # -i local @ARGV = $fileName; while (<>) { s/foo/bar/g; print; } }
Re: Write to filehandle without deleting current text
by jc (Acolyte) on Oct 05, 2010 at 09:36 UTC
    > overwrites existing file. Try >> to append to the file. If appending isn't satisfactory and you want to modify existing content you are going to have to go lower level. Open is no longer your friend. sysopen is the man for the job. perldoc -f sysopen should get you started.
Re: Write to filehandle without deleting current text
by Proclus (Beadle) on Oct 05, 2010 at 10:57 UTC

    My favorite IO module is IO::All

    If you follow the link and look at the examples you can see various file manipulations including adding to a file. It's very practical and IMHO the Perl way.

      When the Read Mode file handler doesn’t to utilize another process you closed the file hander.

      Then open the file a file handler in Write Mode with the same file name and print the contents you will the new (updated) contents in the sample file.

      #!/usr/bin/perl $fileName = "abc"; open(IN, "<", "$fileName") or die "Could not open file $fileName"; read IN, my $new, -s IN, close (IN); $new =~ s/foo/bar/g; # Open the the file in write mode open(OUT, ">", "$fileName") or die "Could not open file $fileName"; # print the new contents print OUT $new; close (OUT);
A reply falls below the community's threshold of quality. You may see it by logging in.