perl use utf8 no utf8 字符长度区别

时间:2022-05-27 20:19:57
1.
[oracle@oadb utf-8]$ cat a1.pl 
my $str="测试更健康";
print length($str);
print "\n";
print $str."\n";

print "11111111111111\n";

[oracle@oadb utf-8]$ perl a1.pl 
15
测试更健康
11111111111111

字符串长度为15

[oracle@oadb utf-8]$ cat a1.pl 
use Encode;
my $str="测试更健康";
print length($str);
print "\n";
print $str."\n";
print "11111111111111\n";
$str=encode_utf8($str);
print length($str);
print "\n";
print $str."\n";
[oracle@oadb utf-8]$ perl a1.pl 
15
测试更健康
11111111111111
30
æµè¯æ´å¥åº·
[oracle@oadb utf-8]$ 

$str=encode_utf8($str); 后字符长度变成30


启用utf8:

[oracle@oadb utf-8]$ perl a2.pl 
5
Wide character in print at a2.pl line 6.
测试更健康
11111111111111

字符串长度变成5


[oracle@oadb utf-8]$ cat a2.pl 
use Encode;
use utf8;
my $str="测试更健康";
print length($str);
print "\n";
print $str."\n";
print "11111111111111\n";
$str=encode_utf8($str);
print length($str);
print "\n";
print $str."\n";
print "11111111111111\n";
[oracle@oadb utf-8]$ perl a2.pl 
5
Wide character in print at a2.pl line 6.
测试更健康
11111111111111
15
测试更健康
11111111111111