Perl 是否已有一个模块提供unicode就绪的printf方法?

Perl 是否已有一个模块提供unicode就绪的printf方法?,perl,unicode,module,printf,Perl,Unicode,Module,Printf,是否已有一个模块提供了一个Unicodereadyprintf方法,该方法采用与内置printf相同的参数,但填充和对齐的宽度适用于Unicode数据 例如: #!/usr/bin/env perl use warnings; use 5.014; use utf8; use charnames qw(:full); binmode STDOUT, ':utf8'; my %hash = ( Peter => "the knight", Rose => "the

是否已有一个模块提供了一个
Unicode
ready
printf
方法,该方法采用与内置
printf
相同的参数,但填充和对齐的宽度适用于
Unicode
数据


例如:

#!/usr/bin/env perl
use warnings; 
use 5.014;
use utf8;
use charnames qw(:full);
binmode STDOUT, ':utf8';

my %hash = (
    Peter => "the knight",
    Rose => "the dressmaker",
    Franc => "the barber",
    John => "the farmer",
    Lucia => "the baroness",
    Merlin => "the s​o​r​c​e​r​e​r",
    Ace => "the two\N{PRIVATE USE TWO} headed dog",
    Elsa => "the miller",
);

for my $key ( sort keys %hash ) {
    printf "%-15.15s %s\n", $hash{$key}, $key;
}

TR11规定的字符宽度的基本支持通过和提供。应该可以在它们上面写一个可行的printf替换,但我不知道完全符合要求需要什么。

如果您担心Unicode的宽度-包括东亚的东西、组合字符和控制cocdes以及所有其他内容-和
printf
,那么正确的答案是您需要来自CPAN模块的
columns
方法

 use Unicode::GCString;
 my $gcs = Unicode::GCString->new($str);
 my $cols = $gcs->columns;
 printf "%*s\n", $cols, $str;
其他示例包括获取字符串的grapheme长度:

use Unicode::GCString;
$gcs = Unicode::GCString->new($str);
my $count = $gcs->length;
此命令用于按grapheme反转字符串:

use Unicode::GCString;
$str = reverse Unicode::GCString->new($str);
use Unicode::GCString;
my $gcs = Unicode::GCString->new($str);
my $piece = $gcs->substr(5, 5);
通过grapheme访问子字符串:

use Unicode::GCString;
$str = reverse Unicode::GCString->new($str);
use Unicode::GCString;
my $gcs = Unicode::GCString->new($str);
my $piece = $gcs->substr(5, 5);

很抱歉,这不在Perl核心中。然而,你能举一个例子说明当前的printf有什么问题吗合計", 1\u 000\u 000\u 000;}@daxim:答案错了。您需要
Unicode::GCString::columns()
他需要做什么。另外,不要自己打电话给
encode
,这几乎总是一个心理错误。@choroba
printf
的错误在于它只对可打印的ASCII有效,而对Unicode无效。那是什么\x92字符串?那不是Unicode。也许我会等到有什么东西预先制作好了。