Well, you're right that you shouldn't be parsing HTML with regular expressions. And since that is the case, it probably won't "just work."
Ideally, you need to be using an HTML parsing and manipulation library. Don't think of HTML as a big string for you to manipulate with text functions: it's a serialized, formatted data structure. You should monkey with it only using a library for that purpose. The various libraries have already fixed the hundreds of bugs that you are likely to face, making it a zillion times more likely that a simple HTML manipulation routine written against them will "just work." The master-level Perl programmers would generally not parse HTML this way, and it's not because they're obsessive and irrational about code quality and purity -- it's because they know that reinventing the wheel themselves is unlikely to yield something that rolls as smooth as the existing machinery.
I recommend HTML::Tree because it functions the way I think of HTML (and XML). I think there are a couple of other libraries that may be more popular.
The real truth is, if you can't even get your program to compile, you need to invest a little more time (a half day or so) figuring out the basics before you come looking for help. You have an error in your syntax for using the s///g regular expression substitution operator, and you need to find out how that is supposed to work before you go any further. It's not hard, and you can find out what you need from the Camel book, or the perlretut manpage, or several other sources. If you don't learn how to debug your program now, then likely any help you receive here is just going to take you to the next syntax error which you won't be able to get past.