Perl:获取要在 cgi-bin 脚本中使用的 stdin 句柄
在 Windows 服务器上使用 perl 5.8.8 我正在编写一个 perl cgi 脚本,使用 Archive::Zip 来动态创建一个必须由用户下载的 zip:这方面没有问题。 zip 在内存中管理,不会使用临时文件或其他方式将物理文件写入磁盘。我想知道如何允许 zip 下载将流写入浏览器。我所做的类似于:
binmode (STDOUT);
$zip->writeToFileHandle(*STDOUT, 0);
但我对这种获取 STDOUT 作为文件句柄的方式感到不安全。它是否正确且稳健?有更好的办法吗?
非常感谢您的建议
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所做的看起来不错!
What you're doing looks fine!
这是展示不耐烦的美德的好机会。
程序员喜欢分解常量文字的重复部分,并将它们放入常量类型容器中。 (但我将在这里简单地使用一个变量,以免分散对重要部分的注意力。)
这看起来并不像是一个胜利,因为现在的代码比以前多得多。一旦您决定不再打印到
STDOUT
而是打印到真实文件,就会得到巨大的回报。或者也许您想要发球台输出。那么你只需要更改一行代码而不是几行。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.)
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.