in reply to Is something wrong with the below Script??

use strict; use warnings; my $infile = ... my $outfile = ... open IN ... open OUT ... while(<IN>){ ... print OUT $_; } close IN; close OUT; rename $infile, $outfile or die "COULDN'T RENAME ($infile) to $outfile +): $!";

Replies are listed 'Best First'.
Re^2: Is something wrong with the below Script??
by koti688 (Sexton) on May 19, 2009 at 04:22 UTC
    why another out File handler??,
    is it not possible to open the same file and replace the sting and save in to it. I need to save in the same file only ..

    Please help

      You might find seek helpful, and in particular this bit:

      Due to the rules and rigors of ANSI C, on some systems you have to do a seek whenever you switch between reading and writing.

      update: You should also think about what will happen to your file if you write directly to your original file and your modified string is shorter or longer than the original.

      Because its short, its easy to understand and it works. Editing a file in-place is possible, but its nontrivial. See Re: Edit File in place for some reasons why.
      It's a good idea to rename your original file to a new name, and to then write out the new version of the file to the original name. This provides an audit trail, and helps to resolve pesky questions when a change you didn't want occurrs...