开发者

Should I pop after each binmode?

开发者 https://www.devze.com 2023-02-04 00:09 出处:网络
When using binmode, should I pop the layers from a possibly previous used binmode? #!/usr/bin/env perl

When using binmode, should I pop the layers from a possibly previous used binmode?

#!/usr/bin/env perl
use warnings;
use 5.012; 
use autodie;

open my $tty, '>:encoding(utf8)', '/dev/tty'; # ...
close $tty;
open $tty, '>:encoding(latin1)', '/dev/tty'; # ...
close $tty;
open $tty, '>:encoding(utf8)', '/dev/tty'; # ...
close $tty;
open $tty, '>:encoding(latin1)', '/dev/tty'; # ...
close $tty;

open $tty, '>:bytes', '/dev/tty';
say "@{[ PerlIO::get_layers( $tty ) ]}"; # unix perlio
close $tty;

say "----------------------------------------";

binmode STDOUT, ':encoding(utf8)'; # ...
binmode STDOUT, ':encoding(latin1)'; # ...
binmode STDOUT, ':encoding(utf8)'; # ...
binmode STDOUT, ':encoding(latin1)'; # ...

binmode STDOUT, ':bytes';
say "@{[ PerlIO::get_layers( *STDOUT ) ]}"; # unix perlio encoding(utf8) /
# utf8 encoding(iso-8859-1) utf8 encoding(utf8) utf8 encoding(iso-8859-1)


binmode STDOUT, ':pop:pop:pop:pop:bytes';
say "@{[ PerlIO::get_layers( *STDOUT ) ]}"; # unix perlio

.

#!/usr/bin/env perl
use warnings;
use 5.012;
use autodie;

open my $tty, '>:encoding(utf8)', '/dev/tty'; # ...
close $tty;

open $tty, '>:raw', '/dev/tty';
say "@{[ PerlIO::get_layers( $tty ) ]}"; # unix
close $tty;

say "----------------------------------------";

binmode STDOUT, ':encoding(utf8)'; # ...

binmode STDOUT, ':raw';
say "@{[ PerlIO::get_layers( *STDOUT ) ]}"; # unix perlio

binmode STDOUT, ':pop:raw';
say "@{[ PerlIO::get_layers( *STDOUT ) ]}"; # unix
开发者_开发百科


:pop is required to pop real layers, such as :encoding(...). So yes, if you want to replace a real layer by another one, then you'll have to :pop.

But note that pushing :raw actually results in a series of pop... And :perlio automatically inserts :unix underneath. So the exact number of pops really depends on the current layers.

As the documentation says itself:

A more elegant (and safer) interface is needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消