“关闭" binmode(STDOUT,“:utf8")本地 [英] "Turn Off" binmode(STDOUT, ":utf8") Locally

查看:395
本文介绍了“关闭" binmode(STDOUT,“:utf8")本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本开头有以下代码段:

I Have The following block in the beginning of my script:

#!/usr/bin/perl5 -w
use strict;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");

在某些子例程中,当有其他编码(来自遥远的子例程)时,在接收西里尔字母或其他字符时,数据将无法正确显示.导致此问题的原因是"binmode".

In some subroutines when there is other encoding(from a distant subroutine), the data will not display correctly, when receiving cyrillic or other characters. It is the "binmode", that causes the problem.

我可以仅在子例程中本地关闭" binmode utf8吗?

我无法删除全局binmode设置,也无法更改远距离编码.

I can't remove the global binmode setting and I can't change the distant encoding.

推荐答案

实现此目的的一种方法是复制" STD句柄,将重复的文件句柄设置为使用:raw层,并将其分配给STD句柄的本地版本.例如,以下代码

One way to achieve this is to "dup" the STD handle, set the duplicated filehandle to use the :raw layer, and assign it to a local version of the STD handle. For example, the following code

binmode(STDOUT, ':utf8');
print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

{
    open(my $duped, '>&', STDOUT);
    # The ':raw' argument could also be omitted.
    binmode($duped, ':raw');
    local *STDOUT = $duped;
    print(join(', ', PerlIO::get_layers(STDOUT)), "\n");
    close($duped);
}

print(join(', ', PerlIO::get_layers(STDOUT)), "\n");

打印

unix, perlio, utf8
unix, perlio
unix, perlio, utf8

在我的系统上.

这篇关于“关闭" binmode(STDOUT,“:utf8")本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆