Perl6 + JVM
tokuhirom
YAPC::Asia 2016
Self Introduction
Amon2,Harriet, Localizer,
Minilla, Test::Requires,
etc.
Perl6?
This is Rakudo Star, a useful,
usable Perl 6 distribution for
"early adopters".
曰く、
useful??
BTW,
Products
• nqp: Perl6 subset, to write Perl6 interpreter
• rakudo: One of Perl6 implementation
VM Support
• Parrot: VM for…
• MoarVM: VM for Perl6
• JVM: VM for Java ← Today’s topic
Why?
Because, I’m using
Java for $DAYJOB
You can use java
libraries, instead of poor
Perl6 libraries.
perl6-j is probably useful
for Java developers
で?
How do I install Perl6?
wget http://rakudo.org/
downloads/star/rakudo-
star-2015.07.tar.gz
tar xzvf rakudo-
star-2015.07.tar.gz
perl Configure.pl
—backends=jvm
—gen-nqp
--prefix=$HOME/perl6
• make
• make install
Easy
brew install rakudo-star
—with-jvm
Then…
Evaluation point as a
Web developer
• Start-up speed
• File access
• JSON processing
• HTTP client
• HTTP Server(Performance)
• Access to mysql
• use java libraries
Start-up speed
time ./install/bin/perl6-j 
-e 'say "Hello, YAPC”’
Hello, YAPC
./install/bin/perl6-j -e 'say "Hello, YAPC”'
17.32s user 0.51s system 254% cpu
6.995 total
Slow!
Is JVM slow?
No!
> javac Hello.java
> time java Hello
Hello
java Hello 0.12s user 0.03s
system 116% cpu 0.130 total
time perl -E 'say "Hello, YAPC"'
Hello, YAPC
perl -E 'say "Hello, YAPC"' 0.04s
user 0.04s system 83% cpu 0.094
total
433 times slower
time /usr/local/bin/perl6-m -e 'say "hello"'
hello
/usr/local/bin/perl6-m -e 'say "hello"' 0.31s
user 0.04s system 97% cpu 0.360 total
2. File Access
say slurp("/etc/
passwd")
It works.
3. JSON
use JSON::Tiny;
(bundled)
> use JSON::Tiny;
> from-json(‘[1,2,true]').perl
[1, 2, Bool::True]
to-json(
{“a"=>5.16,"b"=>false,"c"=>[1..5]}
)
4. HTTP Client
use LWP::Simple;
(Bundled)
> use LWP::Simple
> LWP::Simple.get("http://
64p.org")
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN" "http://www.w3.o$
g/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
useful!
But there’s no legacy
encoding support.
> LWP::Simple.get("http://
google.co.jp")
Unknown encoding 'shift_jis'
5. DB Access
DB Access uses
NativeCall
NativeCall?
FFI - Foreign Function
Interface
Call C API from Perl6
JNA on JVM
ちな Perl5 だと
require DynaLoader;
DynaLoader::dl_install_xsub(
"myfork",
DynaLoader::dl_find_symbol(
DynaLoader::dl_load_file("libc.so"),
'fork'
),
);
myfork();
Joke…
use FFI;
そして Perl6 へ。。
use DBDish;
my $dbh =
DBIish.connect('SQLite', :
database<thefile.sqlite3>);
cp /usr/local/Cellar/sqlite/
3.8.9/lib/libsqlite3.dylib ./
Supported DB:
PostgreSQL
MySQL
SQLite3
use DBIish;
my $dbh =
DBIish.connect('mysql', :host<127.0.0.
1>, :port(3306), :database<mysql>, :us
er<root>, :password(''));
my $sth = $dbh.prepare('SHOW
TABLES');
$sth.execute();
my $arrayref = $sth.fetchall_arrayref();
$arrayref.perl.say;
How do I call JNI
methods?
sub mysql_affected_rows( OpaquePointer
$mysql_client )
returns int32
is native('libmysqlclient')
{ ... }
It’s easy!!!
You can call C APIs!
6. HTTP Server
HTTP::Easy::PSGI
(bundled)
use HTTP::Easy::PSGI;
my $http = HTTP::Easy::PSGI.new(:port(8080));
my $app = sub (%env) {
my $name = %env<QUERY_STRING> || "World";
return [ 200, [ 'Content-Type' => 'text/plain' ], [ "Hello
$name" ] ];
}
$http.handle($app);
with perl6-m
• ab -n 50 -c 1 http://127.0.0.1:8080/
• Requests per second: 5.95 [#/sec] (mean)
with perl6-j
• ab -n 50 -c 1 http://127.0.0.1:8080/
• Requests per second: 10.60 [#/sec] (mean)
10 threads + perl6-j
• ab -c 10 -n 1000 http://127.0.0.1:8080/
• Requests per second: 22.91 [#/sec] (mean)
Single thread + perl6-j
• single thread
• ab -c 10 -n 1000 http://127.0.0.1:8080/
• Requests per second: 17.84 [#/sec] (mean)
ab -n 1000 -c 10
http://127.0.0.1:8080/
Time per request:
550.895 [ms] (mean)
(It doesn’t support
concurrency)
7. Call Java methods
use
java::util::zip::CRC32:fro
m<
java>;
my $crc = CRC32.new();
for 'Hello, Java'.encode('utf-8') {
$crc.update($_)
}
$crc.getValue.say;
it works.
8. GUI
my $frame =
JFrame.new("Helloworl
d");
$frame.setDefaultClose
Operation(1);
my $label = JLabel.new("Hello,
world");
$frame.getContentPane.add($labe
l);
$frame.pack();
$frame.setVisible(True)
Demo
use
My::Own::Class:from<
Java>:jar<hoge.jar>
Conclusion
There is some issues.
But practical than I
thought

Perl6 meets JVM