我应该在每个binmode后弹出吗?

sid*_*com 6 io perl layer binmode

使用binmode时,我应该从可能以前使用过的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
Run Code Online (Sandbox Code Playgroud)

.

#!/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
Run Code Online (Sandbox Code Playgroud)

Dan*_*ger 3

:pop需要弹出真实图层,例如:encoding(...). 所以,是的,如果您想用另一个真实层替换真实层,那么您必须这样做:pop

但请注意,推送:raw实际上会导致一系列弹出...并:perlio自动插入:unix到下面。所以弹出的确切数量实际上取决于当前层。

正如文档本身所说:

需要一个更优雅(更安全)的界面。