in reply to Write to filehandle without deleting current text

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.

  • Comment on Re: Write to filehandle without deleting current text

Replies are listed 'Best First'.
Re^2: Write to filehandle without deleting current text
by k_manimuthu (Monk) on Oct 05, 2010 at 12:48 UTC

    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);