重载字符串和utf8标志

时间:2013-04-30 10:31:39

标签: perl utf-8 overloading stringification

对于重载字符串以及它与utf8标志的交互方式,我有些不明白的事情。

例如,打印以下代码:

n is utf8 at ./test_stringify_utf8.pl line 46.
$t->{name} is utf8 at ./test_stringify_utf8.pl line 47.
t is not utf8 at ./test_stringify_utf8.pl line 48.
Derviş
t is utf8 at ./test_stringify_utf8.pl line 50.

如果我删除say $t,则输出的最后一行也将是t is not utf8

#!/usr/bin/env perl

use utf8;
use Encode qw/is_utf8/;
use strict;

use Modern::Perl '2013';

package Test;
use strict;

sub new {
    my ($class, $name) = @_;

    my $self = { name => $name };
    bless $self, $class;

    return $self;
}

BEGIN {
    my %OVERLOADS = (fallback => 1);

    $OVERLOADS{'""'} = 'to_string';

    use overload;
    overload->import(%OVERLOADS);
}

sub to_string { shift->{name} }


package main;

my $n = "Derviş";
my $t = Test->new($n);

binmode STDOUT, ":utf8";

is_utf8($n)         ? warn "n is utf8"          : warn "n is not utf8";
is_utf8($t->{name}) ? warn '$t->{name} is utf8' : warn '$t->{name} is not utf8';
is_utf8($t)         ? warn "t is utf8"          : warn "t is not utf8";
say $t;
is_utf8($t) ? warn "t is utf8" : warn "t is not utf8";

1 个答案:

答案 0 :(得分:5)

重载的字符串化每次调用时都会返回一个不同的字符串,所以你试图找到一个甚至还不存在的字符串的存储格式。对字符串进行字符串化时,将更新引用的UTF8标志以反映字符串化对象的UTF8。

"".$t也适用于使用say $t;的地方。