end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

Re: YAMLやYAML::Syckでutf8でdecodeしながら読む

だいぶ、以前から
YAMLやYAML::Syckでutf8でdecodeしながら読む - Unknown::Programming
を参考にperlYAMLを使って

my $data = YAML::LoadFile($file);
my $yaml = YAML::Dump($data);
utf8::decode($yaml);
$data = YAML::Load($yaml);

のように、わざわざ utf8::decode()しながら日本語なyamlを開いていましたが...

以下を見て分かる通り、今では、YAML::LoadFile() が、utf8::decode() してるっぽい。

#!/usr/local/bin/perl
use strict;
use utf8;
use Encode;
use YAML;
use Data::Dumper;

main(@ARGV);

# /path/to/messages.yml

sub main {
    my ($path2yaml) = @_;
    my $data = YAML::LoadFile($path2yaml);

    print "#### PRE Encode::encode\n";
    print "Encode::is_utf8() =",
        Encode::is_utf8($data->{Constant}->{error_db}),"\n";
    print $data->{Constant}->{error_db},"\n";

    print "#### POST Encode::encode\n";
    $data->{Constant}->{error_db} =
        Encode::encode('utf8',$data->{Constant}->{error_db});

    print "Encode::is_utf8() =",
        Encode::is_utf8($data->{Constant}->{error_db}),"\n";
    print $data->{Constant}->{error_db},"\n";
}
Constant:
  error_db: 日本語文字列です
    :
$ ./foo.pl /home/endo/messages.yml 
#### PRE Encode::encode
Encode::is_utf8() =1
Wide character in print at ./foo.pl line 19.
日本語文字列です
#### POST Encode::encode
Encode::is_utf8() =
日本語文字列です
$