The two responses before mine do a nice job of answering you question--and I've handled this task in a similar manner.
in reply to Re: Outputing data from mySQL query into format for use with HTML::Template
in thread Outputing data from mySQL query into format for use with HTML::Template
I have since started using Template Toolkit, available on CPAN with additional information available at http://www.tt2.org. I find this template module much more flexible. As a matter of fact, you may pass the database handle directly to the template, saving memory and processing.