title: Perl Read/Write File Handle - Unable to Overwrite
body: <p>I have a Perl Script which performs a specific operation and based on the result, it should update a file.</p>
  
  <p>Basic overview is:</p>
  
  <ol>
  <li>Read a value from the file handle, FILE</li>
  <li>Perform some operation and then compare the result with the value stored in INPUT file.</li>
  <li>If there is a change, then update the file corresponding to File Handle.</li>
  </ol>
  
  <p>When I say, update, I mean, overwrite the existing value in INPUT file with the new one.</p>
  
  <p>An overview of the script:</p>
  
  <pre><code>#! /usr/bin/perl
  
  use warnings;
  use diagnostics;
  
  $input=$ARGV[0];
  open(FILE,"+&lt;",$input) || die("Couldn't open the file, $input with error: $!\n");
  
  # perform some operation and set $new_value here.
  
  while(&lt;FILE&gt;)
  {
  chomp $_;
  $old_value=$_;
  if($new_value!=$old_value)
  {
   print FILE $new_value,"\n";
  }
  }
  
  close FILE;
  </code></pre>
  
  <p>However, this appends the $new_value to the file instead of overwriting it.</p>
  
  <p>I have read the documentation in several places for this mode of FILE Handle and everywhere it says, read/write mode without append.</p>
  
  <p>I am not sure, why it is unable to overwrite. One reason I could think of is, since I am reading from the  handle in the while loop and trying to overwrite it at the same time, it might not work.</p>
  
  <p>Thanks.</p>