Click here to load reader
Upload
keiichi-daiba
View
1.186
Download
2
Embed Size (px)
DESCRIPTION
How to print unicode on erlang with perl and open port
Citation preview
[LT] Regexp, Perl, and Port
id:kdaiba
whoami
id:kdaibaErlang newbiePerl Monger
Yokohama.pmShibuya.pmTokyo.pmNow, setting up "Japan Perl Association"
http://trac.endeworks.jp/trac/tpfj/wiki Infrastructure Engineer
Erlang supports unicode in R12B-5
Try to print utf-8
perl -e 'map{printf "%d,",$_}(unpack "C*","寿");print "\n";'229,175,191,
1> UniString = [229,175,191]."寿"2> io:format("~p~n",[UniString])."寿"ok3> It's looks OK on mac's terminal. But ...
When you run this script...
#!/usr/local/bin/escriptmain(_) -> Item0 = "寿", Item1 = "寿限", Item2 = "寿限無", Item3 = [[229,175,191],[233,153,144],[231,132,161]], io:format("~p~n", [Item0]), io:format("~p~n", [Item1]), io:format("~p~n", [Item2]), [io:format("~p~n", [X]) || X <- Item3].
You'll get returns, like below
"寿"[229,175,191,233,153,144][229,175,191,233,153,144,231,132,161]"寿"[233,153,144][231,132,161]
Do google
There is a page, "Representing Unicode characters in Erlang"http://www.erlang.org/eeps/eep-0010.htmlIt's a "Erlang Enhancement Proposals (EEPs)", #10
This proposal's STATUShttp://www.erlang.org/eeps/ Standards Track EEPAccepted proposalNOT "Proposal is implemented in OTP release R12B-5"
Can't I use utf-8 now ?
use Erlang::Port to see Unicode
I made a perl script to printout utf-8
#!/usr/local/bin/escriptmain(_) -> perlsay:start("./perlsay.pl"), perlsay:say("寿限無、寿限無、五劫の擦り切れ、海砂利水魚、水行末、雲来末、風来末、食う寝る所に住む所、薮ら柑子のぶら柑子、パイポ、パイポ、パイポのシューリンガン、シューリンガンのグーリンダイ、グーリンダイのポンポコピーのポンポコナーの長久命の長助"), perlsay:stop().
You'll get returns on STDERR
寿限無、寿限無、五劫の擦り切れ、海砂利水魚、水行末、雲来末、風来末、食う寝る所に住む所、薮ら柑子のぶら柑子、パイポ、パイポ、パイポのシューリンガン、シューリンガンのグーリンダイ、グーリンダイのポンポコピーのポンポコナーの長久命の長助
Easy erlang code and
-module(perlsay).-export([say/1]).-export([start/1, stop/0]).-import(perlport, [call/2, stop/1]).
say(String) -> call([say, String], perlsay).
start(Script) -> perlport:start(Script, perlsay).
stop() -> perlport:stop(perlsay).
Spaghetti perl script (1/4)
#!/usr/local/bin/perlpackage Erlang::Port::Say;use strict;use warnings;use Erlang::Port;
caller or __PACKAGE__->main(@ARGV);1;
Spaghetti perl code (2/4)
sub main { my $pkg = shift; Erlang::Port->new( sub { my $obj = shift; my $port = shift; my $ret = eval { _my_proc( $obj, $port ) }; $ret = $port->_newTuple( [ $port->_newAtom('error') => $@, ] ) if ($@); $ret; } )->loop();}
Spagetti perl Script (3/4)
sub _my_proc { my $obj = shift; my $port = shift; if ( !UNIVERSAL::isa( $obj, 'ARRAY' ) ) { return $port->_newTuple( [ $port->_newAtom('badarg'), $obj ] ); } my $key = _to_s( $obj->[0] ); if ( !defined($key) || $key ne 'say' ) { return $port->_newTuple( [ $port->_newAtom('badarg'), $obj ] ); } my $str = _to_s( $obj->[1] ); if ( !defined($str) ) { return $port->_newTuple( [ $port->_newAtom('badarg'), $obj ] ); } print STDERR $str, "\n"; $str;}
Spagetti Perl Script (4/4)
sub _to_s { my $obj = shift; if ( defined($obj) && !ref($obj) ) { $obj; } elsif ( $obj && ref($obj) eq 'ARRAY' && @$obj == 0 ) { ""; } elsif ( ref($obj) && UNIVERSAL::isa( $obj, 'Erlang::Atom' ) ) { $$obj; } elsif ( ref($obj) && UNIVERSAL::isa( $obj, 'Erlang::Binary' ) ) { $$obj; } else { undef; }}
"song and dance" too long ?
I'm sorry to be boring
You can use this code like this
#!/usr/local/bin/escript
main(_) -> perlre:start("./perlre.pl"), perlsay:start("./perlsay.pl"), F = perlre:match("赤とんぼ","(\\p{Hiragana}+)"), [perlsay:say(X) || X <- F], perlre:stop(), perlsay:stop().
# perlre.pl is a sample code of Erlang::Port
Finally, you get ...
とんぼ
Twist ending
I need to check B12-R5Today I have bad feelings. So I download B12-R5. It shows,
Eshell V5.6.5 (abort with ^G)But, when I check my mac's erl, it shows
Eshell V5.6.4 (abort with ^G)I made a mumbo jumbo ....