This example shows how to return dynamic content (generated by the Perl/Dancer script) to the user.
Useful if you need to send data to the client, that is not based on a physical file or a variable in memory (or if the data is too big to fit in memory).


Code Highlights

  • The Dancer code is in ./lib/examples/dynamic_content.pm .
  • This HTML template is in ./views/examples/dynamic_content.tt .
  • This is functionally equivalent to the following 'classic' CGI code:
    #!/usr/bin/perl
    use CGI qw/:standard/;
    
    print header(-type=>"text/plain", -attachment=>"foo.txt");
    
    foreach $line ( 1 .. 10000 ) {
    	print "Hello World (line $line)\n";
    }
    
  • Due the Dancer architecture, we can't simply print the data we want to send to the use (as in classic CGI programs). Instead, we supply a callback sub which will send the data to the user.
  • Using send_file with the streaming parameter, we create a subroutine that will send the data back to the user.
  • See the dynamic_content.pm perl module for a detailed example.



Download

Clicking this button will send you a 25MB text file.
The content is dynamically generated by the Dancer code.
Download Dynamic Content