nathaniels has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dancer not displaying Template
by Corion (Patriarch) on Sep 21, 2013 at 07:53 UTC | |
|
Re: Dancer not displaying Template
by choroba (Cardinal) on Sep 21, 2013 at 08:27 UTC | |
|
Re: Dancer not displaying Template
by CountZero (Bishop) on Sep 21, 2013 at 18:57 UTC | |
|
Re: Dancer not displaying Template
by CountZero (Bishop) on Sep 21, 2013 at 18:43 UTC |