title: Replacing a line in a file replaces the line under it. Perl
body: <p>I have a file handler to a file and I'm looking for matches in the lines and replacing the match with a new line. Replacing the lines happen in a subroutine.</p>
  
  <pre><code>sub replace{
     seek(FILE,0,0);
     while(my $line= &lt;FILE&gt;){
         if($line =~ m/SOMEMATCH/){
              $line=~ s/SOMEMATCH/REPLACEMENT/;
              print FILE $line;
         }
      } 
  }
  </code></pre>
  
  <p>When I print the file after calling &amp;replace I find that the wrong line was changed so:</p>
  
  <pre><code>Line 1
  Line 2
  Line 3
  SOMEMATCH
  Line 4
  Line 5
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>Line 1
  Line 2
  Line 3
  SOMEMATCH
  REPLACEMENT
  Line 5
  </code></pre>
  
  <p>What's going on? How do I fix it?</p>