views:

53

answers:

2

Using 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

+4  A: 

What you're doing looks fine!

psmears
Would whoever modded this down like to leave a comment explaining what is wrong with the questioner's code?
psmears
I am very sorry about this... i pushed the wrong button *in error* when approving your answer. In facts, it has no meaning to accept an answer you dislike. When I was aware of the error I clicked on the up and I get the message that the button was locked so i cannot further change the up/down status. let me know how may I help. Sorry again
Daniel
Ah, in that case, no problem, thanks for the explanation :-) It sometimes annoys me on this site when I see people putting in negative votes without taking the time to explain *why* they think something is bad. But a mis-click can happen to anybody ;-)
psmears
@Daniel: I have edited psmears's answer, so now you can change your vote.
Ether
@ether: many thanks to give me the opportunity to fix such a stupid error
Daniel
+3  A: 

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
daxim