Perl5 to Java compiler - first release

This is the first release of the Perl5 to Java compiler.

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

In the github page there is a link to the "jar" file and the lib directories for JVM-specific Perl modules.

The "perlito5.jar" file provides a perl-like command line:

java -jar perlito5.jar -I src5/lib -e ' print "hello, World!\n" '

Note that you don't need to compile a Java file. Perlito5 now compiles the Perl code to JVM bytecode in memory and executes it. Also eval-string is executed as JVM bytecode - there is no interpreter.

Perlito5 is an implementation of the Perl5 language. It is work-in-progress. It provides an impressive coverage of Perl features, but it will not run most existing Perl programs due to platform differences.

More details in the GitHub Perlito5-Java page: https://github.com/fglock/Perlito/blob/master/README-perlito5-Java.md

Perl5 to Java compiler - week 100 - bootstrapping

It's been a 100 weeks since the Perl5 to Java compiler started.

The compiler is now "good enough" to translate itself to Java.

# grab a copy of the project
$ git clone git@github.com:fglock/Perlito.git ; cd Perlito

$ make clean

# create the "perlito5.java" file
$ perl perlito5.pl --bootstrapping -Isrc5/lib -Cjava src5/util/perlito5.pl > perlito5.java

# compile to ".class"
$ time javac -J-Xms2000m -J-Xmx2000m -J-Xss2000m -source 7 perlito5.java
[ scary warnings ]

# create the perlito5.jar file
$ jar -cfe perlito5.jar Main *.class

# quick check
$ java -jar perlito5.jar -v
This is Perlito5 9.021, an implementation of the Perl language.

# run a test
$ java -jar perlito5.jar --bootstrapping -Isrc5/lib -Cjava t5/unit/array.t > x.java ; javac -source 7 x.java ; java Main
[ scary warnings ]
1..35
ok 1 - create array
ok 2 - set element
...

# test the bootstrapping
$ java -jar perlito5.jar --bootstrapping -Isrc5/lib -Cjava src5/util/perlito5.pl > x.java
$ diff x.java perlito5.java
[ no differences ]

The "--bootstrapping" flag tells the compiler that eval-string is not available. Eval-string is not yet implemented.

Android App with the Perl5 to Java compiler

We've had another hackathon at work.

Yati, Bas, Luca and I hacked on the Perlito Perl5-to-Java compiler and also a bit of Perl5-to-JavaScript.

The changes are in GitHub and will be published in the next CPAN release.

The latest cool addition is an Android App example:

specifying primitive data types in Perlito5

I'm trying out this new little benchmark, which imports Java primitive data types into a Perl script:

# misc/benchmark/benchmark_typed_primitive.pl
package long {}
my long $count = 0;
my long $i = 0;
while ( $i < 400 ) {
    my long $j = 0;
    while ( $j < 400 ) {
        my long $k = 0;
        while ( $k < 400 ) {
            $k = $k + 1;
            $count = $count + 1;
        }
        $j = $j + 1;
    }
    $i = $i + 1;
}
my $c = $count;
print "done $c\n";

Comparing with the previous benchmarks:

Plain perl script:

$ time perl misc/benchmark/benchmark_lexical.pl
done 64000000

real    0m4.070s    # base time (1x)
user    0m4.064s
sys 0m0.008s

The same untyped Perl script, running as Perl-in-Java, compilation time excluded:

$ perl perlito5.pl -Isrc5/lib -I. -It -Cjava misc/benchmark/benchmark_lexical.pl > Main.java ; javac Main.java ; time java Main
done 64000000

real    0m0.985s    # 4x faster
user    0m0.626s
sys 0m0.461s

Perl script with primitive types added, running as Perl-in-Java, compilation time excluded:

$ perl perlito5.pl -Isrc5/lib -I. -It -Cjava misc/benchmark/benchmark_typed_primitive.pl  > Main.java ; javac Main.java ; time java Main
done 64000000

real    0m0.102s    # 40x faster
user    0m0.086s
sys 0m0.013s

Just to be sure this is valid Perl:

$ perl -c misc/benchmark/benchmark_typed_primitive.pl
misc/benchmark/benchmark_typed_primitive.pl syntax OK

Compilation time from Perl to Java is:

$ time ( perl perlito5.pl -Isrc5/lib -I. -It -Cjava misc/benchmark/benchmark_typed_primitive.pl  > Main.java ; javac Main.java )

real    0m1.356s
user    0m2.610s
sys 0m0.072s

These command lines run from the root directory of https://github.com/fglock/Perlito checkout; the little benchmark code is at https://github.com/fglock/Perlito/blob/master/misc/benchmark.

Perl5 to Java compiler - using a Perl module from Java

Perlito5 now supports calling Perl subroutines from Java.

For example, a Perl module was compiled to "Main.java", and it can be called using:

 class TestPerl {
    public static void main(String[] args) throws Exception {
        // initialize the Perl module - setup global variables
        // and create the subroutine entries
        Main.init();
        // locate the subroutine main::test
        // and call it with an argument list
        // that is: @res = main::test(123)
        PlObject[] res = Main.apply("main::test", new String[]{ "123" });
        // do something with the results
        for (PlObject s: res) {
            System.out.println("Java result: " + s.toString());
        }
    }
}