while ( my $html_file = $rule->match ) { rename($html_file, "$html_file.bak") or die; open(my $fh_in, '<', "$html_file.bak") or die; open(my $fh_out, '>', $html_file) or die; while (<$fh_in>) { my @lines = <$fh_in>; for (@lines) { #replace #with if (s///){ my $result = $1; #print the file changed and the document path for the included file print OUTFILE "$html_file: $result\n"; } print $fh_out $_; } close($fh_in); close($fh_out); }