Perl Cannot Binmode STDOUT After File Unzip

I need to disable progressive buffering of an HTTP response.

This works for me in Perl, using the file descriptor class:

$|=1; $TIE = tie(*STDOUT,__PACKAGE__); 

Print statements are stored in an array and retrieved using the following:

 $buffer = tied *STDOUT; $buffer = join('', @$buffer); undef $TIE; untie(*STDOUT); 

If the HTTP response is text/html , it displays correctly in the browser.

However, for binary streams, I cannot set binmode to STDOUT after it is untied, and the contents are corrupted.

If I save the HTTP response for the file, or if I do not use the file descriptor class, the binary data is saved.

Any suggestions on how to force raw encoding? Thanks.

+4
source share
1 answer

Something like this work?

 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"; 
+2
source

Source: https://habr.com/ru/post/1415514/


All Articles