我应该在每个binmode后弹出吗?
问题描述:
使用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
。
#!/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
需要弹出真实层,诸如:encoding(...)
。所以是的,如果你想用另一个替换一个真正的图层,那么你必须去:pop
。
但是请注意,推动:raw
实际上会导致一系列的弹出...而:perlio
会自动插入下面的:unix
。所以pop的确切数量确实取决于当前的图层。
由于documentation说自己:需要
更优雅(更安全)接口。