【问题标题】:Should I pop after each binmode?我应该在每个 binmode 之后弹出吗?
【发布时间】:2011-01-14 08:46:22
【问题描述】:

当使用 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

【问题讨论】:

    标签: perl io layer binmode


    【解决方案1】:

    :pop 是弹出真实层所必需的,例如:encoding(...)。所以是的,如果你想用另一个替换一个真实的层,那么你必须:pop。

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

    正如documentation 所说:

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

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 2021-11-08
      • 2015-08-10
      • 1970-01-01
      • 2020-07-04
      • 2021-01-04
      • 2018-06-13
      • 2021-04-17
      相关资源
      最近更新 更多