[Perl] Cache::FileCacheでの日本語の問題【解決編】

同じようなことでつまづく人がいるかもしれないので。
FileCache::get/setでハッシュのキーとして日本語を使おうとするとエラーになります。
これはキーのハッシュ値の計算をするDigest系の関数がAscii文字のみを想定しているからです。
なのでそのまま日本語を渡すのではなく

my $keycode = encode_utf8($keyword);

のようにエンコードしたキーを渡すのが良いでしょう。
なおMD5で例がここにのっています。

Perl 5.8 support Unicode characters in strings. Since the MD5
algorithm is only defined for strings of bytes, it can not be used on
strings that contains chars with ordinal number above 255. The MD5
functions and methods will croak if you try to feed them such input
data:

use Digest::MD5 qw(md5_hex);

my $str = "abc\x{300}";
print md5_hex($str), "\n"; # croaks
# Wide character in subroutine entry

What you can do is calculate the MD5 checksum of the UTF-8
representation of such strings. This is achieved by filtering the
string through encode_utf8() function:

use Digest::MD5 qw(md5_hex);
use Encode qw(encode_utf8);

my $str = "abc\x{300}";
print md5_hex(encode_utf8($str)), "\n";
# 8c2d46911f3f5a326455f0ed7a8ed3b3