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

When you return from a sub, you exit the sub at that point, so the rest of it doesn't get executed.

You want this:

sub get_html { my @array = qw/ happy sad worried /; my $html = <<EOT; <html> <head></head> <body> <select> EOT foreach my $item (@array) { $html .= "<option value='$item'>$item</option>"; } $html .= <<EOT; </select> </body> </html> EOT return $html; }

Disclaimer: It's very bad practice to mix other languages with Perl. If you are printing HTML you should use a templating system, which keeps the HTML in separate files and just plugs in the values of your data. I suggest not writing your own.

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: How do I run a loop within a PSGI application
by tiny_monk (Sexton) on Aug 18, 2015 at 06:23 UTC
    Hello, 1nickt. I ran the code you suggested and it worked wonderfully. Thank you very much and I'm very grateful. :)
Re^2: How do I run a loop within a PSGI application
by tiny_monk (Sexton) on Aug 18, 2015 at 06:06 UTC
    Thank you 1nickt. Would there be a better way to return the entire code without ending prematurely? In a regular Perl script, using CGI, I would use the print command. However, this is totally a different context. I haven't figured out a command that would work.
Re^2: How do I run a loop within a PSGI application
by tiny_monk (Sexton) on Aug 18, 2015 at 06:09 UTC
    Thank you 1nickt. I'll study the code you gave me. Also, thank you for the advise on separating the HTML and Perl codes.