in reply to How do I run a loop within a PSGI application

Here is a way to use a template to generate the html (as 1nickt suggested). I used Template::Tiny, since it is a lightweight templating system whose syntax is compatible with the more feature rich Template::Toolkit. If you have Template::Toolkit installed you should be able to replace use Template::Tiny; with use Template;
#!/usr/bin/perl use strict; use warnings; use diagnostics; use Template::Tiny; my $app = sub { my $html = get_html(); return [ 200, ['Content-Type' => 'text/html'], [$html] ]; }; sub get_html{ my @array; $array[0] = 'happy'; $array[1] = 'sad'; $array[2] = 'worrried'; my $template = do { local $/; <DATA> }; my $tt = Template::Tiny->new(); my $html; $tt->process(\$template, { my_array => \@array }, \$html); return $html; } __DATA__ <html> <head></head> <body> <select> [% FOREACH item IN my_array %]<option value='[% item %]'>[ +% item %]</option> [% END %]</select> </body> </html>