I've recently been toying with moving some Perl code into a web setting using Dancer. Below is some code to generate a list of files in a specific folder and then take in the user's selection. However, when run through the browser it simply generates a blank page. What's confusing for me is that the unrelated code following the template determines whether or not it will generate the page.

This works:

any ['get', 'post'] => '/parsescript' => sub { template 'test.tt'; };

This doesn't:

any ['get', 'post'] => '/parsescript' => sub { template 'test.tt'; my @filelist; my $rawtext; my $rootdir = 'C:\\Users\\Nathaniel\\Corpus\\'; opendir (DIR, $rootdir) or die $!; while (my $file = readdir(DIR) ) { next unless (-f "$rootdir/$file"); next unless ($file =~ /\.txt$/); push @filelist, $file ; } };

Thanks for your help!

UPDATE:Thank you to everyone for their help. As always, one of the best results of posting on here is learning about all the other small mistakes or inefficiencies in your code unrelated to the code-breaking problem. Special thanks to CountZero, it would probably have taken me months to realize template must go at the end of the sub. Turns out my biggest mistake (which I omitted in my post as it seemed irrelevant) was trying to pass a list (rather than a reference to a list) to the template. Thank you all!


In reply to Dancer not displaying Template by nathaniels

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.