[dancer-users] Printing output directly

Chad Wallace cwallace at lodgingcompany.com
Tue May 23 21:54:53 BST 2017


On Mon, 22 May 2017 09:55:12 -0700
Amelia Ireland <aireland at lbl.gov> wrote:

> Hello Dancers,
> 
> Is it possible to print web page output directly using Dancer2?
> 
> I'm using Text::CSV_XS to output DB data as CSV like so:
> 
> while ( my $row = $sth->fetch ) {
> $csv->print ( $io, $row );
> }
> 
> where $io is a file handle, socket, etc.
> 
> At the moment, I'm creating a temp file, writing the data, and then
> using send_file to dispatch the data. Ideally I'd like to just print
> it straight to the web output, but (e.g.) setting $io to *STDOUT does
> not work, and there is no serialiser for csv or plain text.
> 
> Is there a shortcut that I'm missing?

You set the content type and return your output as a string.

Something like this:

    get '/data.csv' => sub {
	my $output = '';
        while ( my $row = $sth->fetch ) {
            # TODO: append to $output instead of printing
        }

        content_type 'text/csv';
        return $output;
    };



-- 

C. Chad Wallace, B.Sc.
The Lodging Company
http://www.lodgingcompany.com/
OpenPGP Public Key ID: 0x262208A0



More information about the dancer-users mailing list