Perl:获取要在 cgi-bin 脚本中使用的 stdin 句柄

发布于 2024-09-06 04:13:18 字数 322 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-09-13 04:13:18

你所做的看起来不错!

What you're doing looks fine!

兔姬 2024-09-13 04:13:18

这是展示不耐烦的美德的好机会。

程序员喜欢分解常量文字的重复部分,并将它们放入常量类型容器中。 (但我将在这里简单地使用一个变量,以免分散对重要部分的注意力。)

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');

这看起来并不像是一个胜利,因为现在的代码比以前多得多。一旦您决定不再打印到 STDOUT 而是打印到真实文件,就会得到巨大的回报。或者也许您想要发球台输出。那么你只需要更改一行代码而不是几行。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文