为什么编码会删除参数? [英] Why does encode delete the argument?

查看:80
本文介绍了为什么编码会删除参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 CHECK 设置为真值,为什么编码删除传递的参数?

Why does encode delete the passed argument, if CHECK is set to a true value?

#!/usr/bin/env perl
use warnings;
use strict;
use utf8;
use Encode;

my $decoded = 'h';
if ( eval { encode( 'utf-8', $decoded, 1 ); 1 } ) {
    print "|$decoded|\n";    # prints ||
}


推荐答案

在这种情况下,您反复将数据块传递给 encode decode 。想法是该函数将删除它已翻译的字符串的一部分,而您只需要在剩余的内容后面追加下一块即可。对于处理可能分成两个块的多字节编码很有用。

It is for use in the case where you are repeatedly passing chunks of data to encode or decode. The idea is that the function will remove the part of the string that it has translated, and you need only append the next chunk to what is left. It is useful for handling multi-byte encodings that may be split across two chunks.

如果您不希望出现这种情况,则可以使用 Encode :: LEAVE_SRC 位进入第三个参数。像这样

If you don't want this behaviour then you can OR the Encode::LEAVE_SRC bit into the third parameter. Like this

use utf8;
use strict;
use warnings;

use Encode qw/ encode decode FB_CROAK LEAVE_SRC /;
use Data::Dump;

my $decoded = 'ABC';
dd $decoded;
my $encoded = encode( 'UTF-8', $decoded, FB_CROAK | LEAVE_SRC );
dd $decoded;
dd $encoded;

输出

"ABC"
"ABC"
"ABC"

这篇关于为什么编码会删除参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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