|more useful options|
From the Dancer docs about the template function:
Returns the response of processing the given template with the given parametersIn other words, whatever the sub returns goes to the client's browser, which is not necessarily what was returned by template. Hence in your first example you indeed see a webpage and in the second example you "see" the value of the while loop once it ends. As that is unlikely to be a properly formed web-page there is nothing to see. So put the call to template at the very end of the sub.
And don't forget to pass @filelist to the template function (read the docs on how to do this).
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey JamesMy blog: Imperial Deltronics