开发者

Perl: getting handle for stdin to be used in cgi-bin script

Using开发者_C百科 perl 5.8.8 on windows server I am writing a perl cgi script using Archive::Zip with to create on fly a zip that must be download by users: no issues on that side. The zip is managed in memory, no physical file is written to disk using temporary files or whatever. I am wondering how to allow zip downloading writing the stream to the browser. What I have done is something like:

binmode (STDOUT);
$zip->writeToFileHandle(*STDOUT, 0);

but i feel insecure about this way to get the STDOUT as file handle. Is it correct and robust? There is a better way?

Many thanks for your advices


What you're doing looks fine!


This is a good chance to demonstrate the virtue of Impatience.

Programmers like to factor out repetition of constant literals, and put them into constant type containers. (But I'll simply use a variable here in order to not distract from the important part.)

use IO::File qw();
my $handle = bless(\*STDOUT => 'IO::File')
    or die $OS_ERROR;
# Why not just `$handle = STDOUT`? Code above is necessary
# because using bare STDOUT invokes on IO::Handle only
# which does not have the binmode method.
⋮
$handle->binmode(1);
$handle->print('something');

This is not look like a win because there's much more code now than before. The big pay-off comes as soon as you decide that you don't want to print to STDOUT anymore after all, but to a real file. Or perhaps you want to tee the output. Then you only need to change one line of code instead of several.

my $handle = IO::File->new('/var/log/cgi', 'a')
    or die $OS_ERROR;
# method calls on $handle stay the same as before
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜