Net::Twitterで得られた文字列中の \uXXXX で困った
流行のtwitter.comというものをやってみようと思い、とりあえずCPANでNet::Twitterをゲット。以下のようなプログラムでとりあえずpublic_timelineを見ようと思いました。
use strict; use warnings; use Data::Dumper; use Net::Twitter; use Encode; my $twit = Net::Twitter->new; print Dumper($twit->public_timeline());
ところが、これで得られるテキスト部分が'\\uXXXX'という形式(つまり文字列としては \uXXXX という形式)になります。ええと、これをutf-8に変換するのは、次のようにpackを使うんでしょうか?
use strict; use warnings; binmode(STDOUT, ':utf8'); my $s = 'My name is \\u6D69.'; $s =~ s/\\u(....)/ pack("U1", hex($1)) /eg; print $s;
もっと「スジのよい」方法をご存じの方はぜひご教示ください。
追記:
とりあえず現状は、こんな感じで動いています。何だか\のエスケープ変だ。
追記:
うーん、Net::Twitter(JSON::Any)経由よりも、直接RSS見た方が楽そうだな…。でももう眠い。ぐー。
追記:
xcezxさん経由→typesterさん経由→miyagawaさんのところを教えていただきました。Encode::JavaScript::UCSでdecode('JavaScript-UCS', $json)だそうです。感謝感謝。
C:\work> cpan Encode::JavaScript::UCS
↑これでインストール。↓で実行。
use strict; use warnings; use Data::Dumper; use Net::Twitter; use Encode; use Encode::JavaScript::UCS; my $timeline = Net::Twitter->new->public_timeline(); print map { $_->{text} . "\n" } @$timeline; print "\n"; print map { decode('JavaScript-UCS', $_->{text}) . "\n" } @$timeline; __END__
実行結果(の抜粋)です。
\u304a\u306f\u3088\u3002\u5915\u3079\u306f\u30b3\u30bf\u30c4\u3067\u884c\u304d\u5012\u308c\u3066\u305f\u307f\u305f\u3044\u3002\u4e8c\u65e5\u9154\u3044\u306f\u306a\u3044\u3088\u3002 watched a dvd movie with the family titled a crusade with blue jeans. the scenery was very nice I am wondering about who that "Some Cat" reference was to ... getting ready to go out for the evening... listening to reggae at work on a sunday... so sleepy today...aargh. (http:\/\/www.example.com): I just recorded 'DMX - Party Up (up in here)' from '.977 The Hitz Channel' Morgen habe ich keine Unterricht...SUPER! おはよ。夕べはコタツで行き倒れてたみたい。二日酔いはないよ。 watched a dvd movie with the family titled a crusade with blue jeans. the scenery was very nice I am wondering about who that "Some Cat" reference was to ... getting ready to go out for the evening... listening to reggae at work on a sunday... so sleepy today...aargh. (http:\/\/www.example.com): I just recorded 'DMX - Party Up (up in here)' from '.977 The Hitz Channel' Morgen habe ich keine Unterricht...SUPER!
あとは、"\/"→"/"の解決だけか。