编码 - 字符串字节长度
file1.pl
:
use strict;
use warnings;
use Encode;
my @flist = `svn diff --summarize ...`;
foreach my $file (@flist) {
my $foo = "$one/$file";
use bytes;
print(bytes::length($one)."\n");
print(bytes::length($file)."\n");
print(bytes::length($foo)."\n");
}
# 76
# 31
# 108
我已经使用相同的主逻辑 和file2.pl
。但是在file2.pl
中,输出为:
# 76
# 31
# 110 <-- ?
两个文件具有相同的编码(ISO-8859-1)。对于与file1.pl
中的结果相同的结果,我要
my $foo = "$one/".decode('UTF-8', $file);
在file2.pl
中使用。 decode('utf-8',$ file)
in file2.pl
中的decode('utf-8',$ file)的原因可能是什么?似乎与如果我不解码?怎么办?谢谢。
Perl v5.10.1
I've file file1.pl
:
use strict;
use warnings;
use Encode;
my @flist = `svn diff --summarize ...`;
foreach my $file (@flist) {
my $foo = "$one/$file";
use bytes;
print(bytes::length($one)."\n");
print(bytes::length($file)."\n");
print(bytes::length($foo)."\n");
}
# 76
# 31
# 108
and file2.pl
with the same main logic. But in file2.pl
the output is:
# 76
# 31
# 110 <-- ?
Both files have the same encoding (ISO-8859-1). For the same result as in file1.pl
I've to use
my $foo = "$one/".decode('UTF-8', $file);
in file2.pl
. What could be the reason for that difference or the requirement of decode('UTF-8', $file)
in file2.pl
? Seems to be related to What if I don't decode? but in which manner and only in file2.pl
? Thx.
Perl v5.10.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用字节。
bytes ::长度
获得字符串内部存储的长度。没用的。$一个
和$ file
包含使用不同内部存储格式存储的字符串。需要转换一个以进行连接。Don't use bytes.
bytes::length
gets the length of the internal storage of a string. It's useless.$one
and$file
contained strings stored using different internal storage formats. One needed to be converted for a concatenation to occur.