The problem is that each time you append the new line to $outdata, and then run your replacements against the entire text (not just the newly added line). To fix it, either run your replacements once outside of the loop or run them only on the new line. Actually, I would suggest another approach without the loop.
sub find_replace { my $filename = shift; open (my $infile, "$dir/$filename") or die "Can't open file: $!"; local $/ = undef; my $outdata = <$infile>; close $infile; $outdata =~ s{../CLRIS/}{}g; $outdata =~ s{../../menu/}{}g; $outdata =~ s{../../Images}{Images}g; $outdata =~ s{<cfmail}{<!--- <cfmail}g; $outdata =~ s{</cfmail>}{</cfmail> --->}g; open (my $outfile, "+>$dir/$filename") or die "Can't open file: $! +"; print $outfile "$outdata"; close $outfile; }
Oh, while I'm looking at it, I suspect you don't actually mean s{../CLRIS/}{}g;. It seems much more likely that you mean s{\.\./CLRIS/}{}g;

In reply to Re^3: Unexpected results from a regex replacement by Eimi Metamorphoumai
in thread Unexpected results from a regex replacement by yacoubean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.