It's always a good (first) idea to separate code from html, using a template-tool like HTML::Template. You can simplify your work using HTML::Entities, instead of doing the work with your own regexes.
Comment on Re: Making this script cleaner and better