title: How to replace string and preserve its uppercase/lowercase
body: <p>I want to replace one string with the other in Perl; both are of the same length. I want to replace all occurrences of the string (case insensitive), but I want that the case of the letter will be preserved. So if the first letter was upper case, the first letter after the replacement will be upper case also.</p>
  
  <p>For example, if I want to replace "foo" with "bar", so I want that</p>
  
  <pre><code>foo ==&gt; bar
  Foo ==&gt; Bar
  FOO ==&gt; BAR
  </code></pre>
  
  <p>Is there a simple way to do this in Perl?</p>