Here are a couple suggestions for cleaning up that code:
HTML::Template - get that HTML code out of your perl code. Too difficult to read. Others may suggest perldoc:://Text::Template, or a myriad of others. It doesn't matter too much to me - you can even use perldoc:://XML::Twig for all I care (make sure you're 100% XHTML-compliant if you want to go for this one...). Just pick something.
CGI::Application - put some order to it all. This one you might already be using, but if you were, it would have pointed you to investigate HTML::Template. Note that while I think there are some serious shortcomings to CGI::Application, it is still revolutionary over older CGI programming strategies.
This should help make this a wee bit easier to handle. And thus easier for others to help with. Also, try to firm up what your question is, if possible. Especially with CGI scripting - it's harder for others to reproduce than other types of programs that we can take the source and run easily.