Don't so much search-and-replace as read-possibly change-print.
- As you go through the file with HTML::Parser (or whatever) remember if you're in an area where you want to do the replacement, or not (not in titles, yes in free text, etc) and print it to the new file.
- Then your text handler subroutine only needs to check that variable, do the search-and-replace on the text string and print it regardless of if the substitution has been made.