in reply to delete and substitute
Take a serious look at HTML::TreeBuilder or some of the other HTML munging modules in CPAN. You really don't want to be doing that sort of work with hand rolled code!