【发布时间】:2015-11-23 23:50:25
【问题描述】:
我正在开发一个带有大字典的 Android 文字游戏 -
单词(超过 700 000 个)作为单独的行保存在文本文件中(然后放入 SQLite 数据库)。
为了保护我的字典,我想用 md5 对所有长度超过 3 个字符的单词进行编码。 (我不会混淆简短的单词和带有罕见俄语字母ъ 和э 的单词,因为我想在我的应用中列出它们)。
这是我尝试在 Mac Yosemite 上使用 perl v5.18.2 运行的脚本:
#!/usr/bin/perl -w
use strict;
use utf8;
use Digest::MD5 qw(md5_hex);
binmode(STDIN, ":utf8");
#binmode(STDOUT, ":raw");
binmode(STDOUT, ":utf8");
while(<>) {
chomp;
next if length($_) < 2; # ignore 1 letter junk
next if /жы/; # impossible combination in Russian
next if /шы/; # impossible combination in Russian
s/ё/е/g;
if (length($_) <= 3 || /ъ/ || /э/) { # do not obfuscate short words
print "$_\n"; # and words with rare letters
next;
}
print md5_hex($_) . "\n"; # this line crashes
}
如您所见,我必须在我的 Perl 脚本的源代码中使用西里尔字母 - 这就是我将 use utf8; 放在其顶部的原因。
但我真正的问题是 length($_) 报告的值太高(可能报告字节数而不是字符数)。
所以我尝试添加:
binmode(STDOUT, ":raw");
或:
binmode(STDOUT, ":utf8");
但是脚本随后在 print md5_hex($_) 的行处以 Wide character in subroutine entry 终止。
请帮我修复我的脚本。
我运行它:
perl ./generate-md5.pl < words.txt > encoded.txt
为了您的方便,这里是示例 words.txt 数据:
а
аб
абв
абвг
абвгд
съемка
【问题讨论】:
标签: perl unicode utf-8 md5 cyrillic