解开文件句柄后,Perl无法Binmode STDOUT [英] Perl Cannot Binmode STDOUT After Untie Filehandle
问题描述
我需要禁用HTTP响应的渐进式缓冲.
I need to disable progressive buffering of an HTTP response.
我已经使用文件句柄类在Perl中工作了:
I've got this working in Perl using a file handle class:
$|=1;
$TIE = tie(*STDOUT,__PACKAGE__);
打印语句存储在数组中,并通过以下方式检索:
Print statements are stored in an array and are retrieved via the following:
$buffer = tied *STDOUT;
$buffer = join('', @$buffer);
undef $TIE;
untie(*STDOUT);
如果HTTP响应为text/html
,它将正确显示在浏览器中.
If the HTTP response is text/html
, it correctly displays in the browser.
但是,对于二进制流,我不能在STDOUT
上设置binmode
并将其解开并且内容损坏.
However, for binary streams, I can't set binmode
on STDOUT
after it is untied, and the contents are corrupted.
如果将HTTP响应保存到文件,或者不使用文件句柄类,则将保留二进制数据.
If I save the HTTP response to a file, or if I do not use a file handle class, the binary data is preserved.
关于如何强制进行原始编码的任何建议?谢谢.
Any suggestions on how to force raw encoding? Thanks.
推荐答案
像这样的工作吗?
use strict;
use warnings;
use IO::Handle;
my $io = IO::Handle->new;
my $fh = $io->fdopen(fileno(STDOUT),"w");
$fh->autoflush(1);
my $TIE = tie( $fh ,__PACKAGE__);
sub TIESCALAR { };
binmode($fh);
print $fh "Foo";
这篇关于解开文件句柄后,Perl无法Binmode STDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!