我有檔案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?似乎與如果我不解碼怎么辦?但以何種方式且僅在file2.pl?謝謝。
Perl v5.10.1
uj5u.com熱心網友回復:
不要使用位元組。
強烈建議不要將此模塊用于除錯目的以外的任何用途。
bytes::length獲取字串內部存盤的長度。沒用的。
造成這種差異的原因可能是什么
$one并$file包含使用不同內部存盤格式存盤的字串。一個需要轉換才能發生串聯。
use strict;
use warnings;
use feature qw( say );
use bytes qw( );
use Encode qw( encode );
sub dump_lengths {
my $s = shift;
say
join " ",
length( $s ),
length( encode( "UTF-8", $s ) ),
bytes::length( $s );
}
# ------ Length of string
my $x = chr( 0xE9 ); # | ---- Length of its UTF-8 encoding
my $y = chr( 0x2660 ); # | | -- Length of internal storage
# | | |
dump_lengths( $x ); # 1 2 1
dump_lengths( $y ); # 1 3 3
my $z = $x . $y;
dump_lengths( $z ); # 2 5 5
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494018.html
