[cgiapp] Re: How to send pdf file?

Aristotle Pagaltzis pagaltzis at gmx.de
Sun Feb 3 14:17:30 EST 2008


* petr.vojkovsky at centrum.cz <petr.vojkovsky at centrum.cz> [2008-02-03 11:55]:
> @fileholder = <FILE>;  
> print @fileholder;

* Jesse Erlbaum <jesse at erlbaum.net> [2008-02-03 14:50]:
>    @fileholder = <FILE>;
>    return join("", @fileholder);

Note that this is a really bad way to slurp a whole file. In the
worst case (2nd example) it will consume over thrice the size of
the file in memory.

In general, if you do slurp an entire file, you should do it
using `read`:

    read *FILE, $fileholder, -s *FILE;

But if you do that and then `return $fileholder`, you’ll still
get double the size of the file in memory consumption, because
Perl makes a copy of the scalar to return it.

The right approach in such cases is either passing around a file
handle (so the output can be streamed directly) or a reference
(but then the code which outputs it must still be written
carefully to avoid making a copy).

In case of a CGI::App app, the answer is to pass a file handle,
which is done using CGI::Application::Plugin::Stream. That will
also conveniently take care of setting all the requisite headers.

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle Pagaltzis // <http://plasmasturm.org/>


More information about the cgiapp mailing list