Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all,

How am I read a file, find the match regular expression and overwrite to the same files.

open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat";
open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat";
while (<DESTINATION_FILE>)
{
# print DESTINATION_FILE_2 "jessy\n";
 chomp($_);
# print "$_ is $hash{$_}\n";
   $line="CLIPPED_PIN_COUNT";
  #  print NEW_DESTINATION_FILE "$_\n";
 #if ($_=~/^$line/)
 if (/^CLIPPED_PIN_COUNT/)
 {
  print "original mathched:",$_,"\n";
  $var=$hash{$line};
  print "var is:",$var,"\n";
#Print out from tmptravl file 
  print "Obtain From tmptravl Fle:",$_,"\n";
#Split out
   @field_new=split/:/,$_;
# print @field;
   push @new_array_new, $field_new[$i];
   print "field",$i,":",$field_new[$i],"\n";
   $i ++;
   push @new_array_new, $field_new[$i];
   print "field",$i,":",$field_new[$i],"\n";
 
#Print out from original file
  print "Obtain From Original File:",$var,"\n";
  s/$_/$field_new[0]:$var/g;
  print "After substituition:",$_,"\n";
  print "-----------------------------------------\n";
         print NEW_DESTINATION_FILE "$_\n";
#   print DESTINATION_FILE,"s/$field_new[$1]/$var/";
#Subsititue original attribute to tmptravl attribue
  close (DESTINATION_FILE);
  close (NEW_DESTINATION_FILE);
 }

}

Do I need to open two file handlers? My purpose is to overwrite the attribute i get from tmptravl.dat and write to new_tmptravl.dat? However, when I write to new_tmptravl.dat, only the new substitution is written in my new_tmptravl.dat? May I know what is the root cause for it?
Thanks.

Can you put your code within code tags?

tyler_durden