1 of 24

Perlito Perl programming language compiler

Perlito, a compiler collection that implements Perl 5 and Perl 6.

Perl to Java compiler

2 of 24

PPC Portable Perl Compiler

3 of 24

PPC Portable Perl Compiler

Flávio S. Glock

Booking.com

PPC Team

4 of 24

Perl to Java compiler

  • Why?

Fun: Hackathons, community

Technical: Typed variables; explore the limits

Business: Hadoop, Android

5 of 24

Perl to Java compiler

  • Install from GitHub

https://github.com/fglock/Perlito

  • Install from CPAN

https://metacpan.org/pod/Perlito5

6 of 24

Not so easy in Java world

  • No timely destruction
    • DESTROY
    • Auto-close files
  • No XS
  • Not Unix
  • Long application startup time

7 of 24

Nice things in Java world

  • Lot of modules
  • Threads
  • Memory management

8 of 24

Work in progress

  • overload
  • encoding

9 of 24

Perl5 Grammar

token stmt_yadayada {

'...'

{

$MATCH->{capture} = Perlito5::AST::Apply->new(

code => 'die',

namespace => '',

arguments => [ Perlito5::AST::Buf->new( buf => 'Unimplemented' ) ],

);

}

};

link: https://github.com/fglock/Perlito

10 of 24

Perl5 BEGIN blocks

BEGIN {

for my $i ("first", "second", "third") {

*{"main::sub_$i"} = sub {

say "This is $i";

};

}

}

java -jar perlito5.jar -I src5/lib -Cperl5 demo_begin.pl

11 of 24

Perl5 eval-string

say "input: ";

my $x = <>;

eval <<~'EOT';

chomp $x;

say '$x = ', $x;

say "next: ", ++$x;

EOT

java -jar perlito5.jar -I src5/lib demo_eval.pl

java -jar perlito5.jar -I src5/lib -Cjava demo_eval.pl

12 of 24

Perl to Java compiler

  • Compiler generates plain Java code with no dependencies

13 of 24

Perl to Java compiler

// print “hello, World!\n”;

PlCORE.print(

PlCx.VOID,

PlCx.STDOUT,

new PlArray(

new PlString("hello, World!" + (char)10)

)

);

14 of 24

Perl to Java compiler

  • Shadow modules
    • Internals
      • PerlitoX::strict
    • Platform specific - XS replacement
      • PerlitoX::Java::MIME::Base64

15 of 24

Perl to Java compiler

  • Java imports should be hidden in modules
    • Just like we move XS away from view
    • use PerlitoX::Java namespace
    • Main program should be plain perl

16 of 24

Java Extensions

use feature 'say';

use strict;

package Calendar { import => "java.util.Calendar" }

package TimeZone { import => "java.util.TimeZone" }

my Calendar $calNewYork = Calendar->getInstance();�

$calNewYork->setTimeZone(TimeZone->getTimeZone("America/New_York"));

my $hour = $calNewYork->get(Calendar->HOUR_OF_DAY);

my $min = $calNewYork->get(Calendar->MINUTE);

say "Time in New York: $hour:$min";

17 of 24

Dual object system

  • Perl and Java objects
  • Typed variables
  • Import Java classes
    • Integrates with HBase, Android

https://github.com/fglock/Perlito/blob/master/misc/Java/TestThread2.pl

perl perlito5.pl -Isrc5/lib -Cjava misc/Java/TestThread2.pl > Main.java ; javac Main.java ; java Main

18 of 24

Perl to Java compiler

  • Compiler Internals - Hackathon activities

Command line compiler

Runtime library

Parser development

DSL for Java imports

Compiler optimization

19 of 24

Perl to Java compiler

  • Where is it used

Android test suite

Code extracted from “lib/Bookings/Tools/UserAgent.pm”

(code is not shared with main.git)

http-tests/src/test/perl/UserAgent.pm

http-tests/src/test/java/com/booking/common/http/UserAgentPerl.java

20 of 24

Perl to Java compiler

  • Tools - Hackathon projects

Android development environment

Android specific targeting

Android <> Java SE

Integration with “Android Studio”

- Gradle task

- special “-Candroid” compiler switch

21 of 24

Perl to Java compiler

  • Conclusion

about 10 people have worked in compiler internals.

We’ve used the Perl to Java compiler in places where rewriting code in Java was painful, such as in regex-heavy business rules.

22 of 24

Perl to Java compiler

  • Current state

Still fun to work with, developing a community

Still technical challenges

Still going after more business

java -jar perlito5.jar -I src5/lib -Cjava camel.pl

23 of 24

Perl to Java compiler

  • Thanks!

Pugs, v6.pm: Audrey, "putter"

Perlito5: Jesse, “pmurias”, Shlomi, Simon

Perlito5-Java: Bosko, Yati, Bruno, Bas, Ovidiu,

Emmanuel, Frederico, Imran, Luca�

24 of 24

Perlito Perl language compiler

Flávio S. Glock