charnames :: viacode()的短名称?

时间:2013-01-10 22:32:40

标签: perl unicode

是否有charnames::viacode()的版本会返回代码点的短名称,即use charnames ':short'生效时可以使用的名称?

use charnames ':short';

my $x = ord("\N{Cyrillic::es}");

say "long name:  ", charnames::viacode($x); # prints "CYRILLIC SMALL LETTER ES"
say "short name: ", ???($x);                # want: Cyrillic:es

1 个答案:

答案 0 :(得分:1)

charnames文件

SCRIPTNAME:CHARNAME

搜索

  • SCRIPTNAME CAPITAL LETTER CHARNAME(如果CHARNAME不是全部小写)
  • SCRIPTNAME SMALL LETTER CHARNAME(如果CHARNAME全部小写)
  • SCRIPTNAME LETTER CHARNAME

所以你可以使用

sub short_name {
   my ($long_name) = @_;
   return "$1:\U$2" if $long_name =~ /^(\S+) CAPITAL LETTER (\S+)\z/;
   return "$1:\L$2" if $long_name =~ /^(\S+) SMALL LETTER (\S+)\z/;
   return "$1:$2"   if $long_name =~ /^(\S+) LETTER (\S+)\z/;
   return $long_name;
}