title: Multiline search replace with Perl
body: <p>I know this kind of questions have been asked already many times before. The reason why I come here again is that I feel like I've missed something simple and fundamental.</p>
  
  <p>Is it possible to make this kind of search-replace routine better. For example without opening same file twice. Also speed related advices are welcome.</p>
  
  <p>Please notice that this works with multiline matches and replaces also multiline strings.</p>
  
  <pre><code>#!/bin/perl -w -0777
  
  local $/ = undef;
  
  open INFILE, $full_file_path or die "Could not open file. $!";
  $string =  &lt;INFILE&gt;;
  close INFILE;
  
  $string =~ s/START.*STOP/$replace_string/sm;
  
  open OUTFILE, "&gt;", $full_file_path or die "Could not open file. $!";
  print OUTFILE ($string);
  close OUTFILE;
  </code></pre>