Skip to content

Instantly share code, notes, and snippets.

@niratama
Created October 17, 2013 10:54
Show Gist options
  • Save niratama/7022910 to your computer and use it in GitHub Desktop.
Save niratama/7022910 to your computer and use it in GitHub Desktop.
XslateとUTF-8の挙動一覧
#!/usr/bin/env perl
use strict;
use utf8;
use Encode qw(encode decode);
use Text::Xslate;
my $template = <<'__HERE__';
: my $xkey = '日本語';
: my $ykey = dec('日本語');
whash:wkey:<: $whash[$wkey] :>
whash:ukey:<: $whash[$ukey] :>
whash:xkey:<: $whash[$xkey] :>
whash:ykey:<: $whash[$ykey] :>
whash:str:<: $whash['日本語'] :>
whash:dec(wkey):<: $whash[dec($wkey)] :>
whash:dec(ukey):<: $whash[dec($ukey)] :>
whash:dec(str):<: $whash[dec('日本語')] :>
whash:enc(wkey):<: $whash[enc($wkey)] :>
whash:enc(ukey):<: $whash[enc($ukey)] :>
whash:enc(str):<: $whash[enc('日本語')] :>
uhash:wkey:<: $uhash[$wkey] :>
uhash:ukey:<: $uhash[$ukey] :>
uhash:xkey:<: $uhash[$xkey] :>
uhash:ykey:<: $uhash[$ykey] :>
uhash:str:<: $uhash['日本語'] :>
uhash:dec(wkey):<: $uhash[dec($wkey)] :>
uhash:dec(ukey):<: $uhash[dec($ukey)] :>
uhash:dec(str):<: $uhash[dec('日本語')] :>
uhash:enc(wkey):<: $uhash[enc($wkey)] :>
uhash:enc(ukey):<: $uhash[enc($ukey)] :>
uhash:enc(str):<: $uhash[enc('日本語')] :>
__HERE__
my %whash = ('日本語' => 'にほんご');
my %uhash = map { encode('utf-8', $_) } %whash;
my $wkey = '日本語';
my $ukey = encode('utf-8', $wkey);
my $tx = Text::Xslate->new(
function => {
dec => sub { shift },
enc => sub { encode('utf-8', shift) },
});
print encode('utf-8', $tx->render_string($template, {
whash => \%whash,
uhash => \%uhash,
wkey => $wkey,
ukey => $ukey,
}));
__END__
$ perl test.pl
whash:wkey:にほんご
whash:ukey:
whash:xkey:
whash:ykey:にほんご
whash:str:
whash:dec(wkey):にほんご
whash:dec(ukey):
whash:dec(str):にほんご
whash:enc(wkey):
whash:enc(ukey):
whash:enc(str):
uhash:wkey:
uhash:ukey:にほんご
uhash:xkey:にほんご
uhash:ykey:
uhash:str:にほんご
uhash:dec(wkey):
uhash:dec(ukey):にほんご
uhash:dec(str):
uhash:enc(wkey):にほんご
uhash:enc(ukey):
uhash:enc(str):にほんご
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment