关于重载字符串化以及它如何与 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";