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());
        }
    }
}

Perl5 to Java compiler - first little script

For the first time, Perlito5-Java can now run an unmodified script - "rc-forest-fire" from the perl6-bench project.

I've experimented running the benchmark on the several Perlito5 platforms, and on perl itself:

# Perl5-in-Java - including compilation time
time ( touch Test.class ; rm Test.class ; perl perlito5.pl -Isrc5/lib -Cjava rc-forest-fire.pl > Test.java ; javac Test.java ; java Test 20 20 1000 )
real 6.9s
user 5.7s


# Perl5-in-Java - precompiled
time java Test 20 20 1000
real 5.7s
user 3.7s


# Perl5-in-JS - just-in-time compile
time node perlito5.js -Isrc5/lib rc-forest-fire.pl 20 20 1000
real 5.9s
user 4.8s


# perl
time perl rc-forest-fire.pl 20 20 1000
real 5.3s
user 1.1s

I've tested with javac 1.7.0_79, nodejs 0.10.25, perl 5.20.1 in Ubuntu.

There is a lot to optimize in the Java code generator, and Javascript could do a little better, too. But I'm very happy that the performance is not much worse in the first try!

Perl5 to Java compiler - integrating with java.lang.Thread

Here is another experiment with calling Java libraries from Perlito5-Java; using JVM threads:

package Java::Thread {
    import => "java.lang.Thread"
};

my Java::Thread $thread1 = new Java::Thread(
    sub {
        for my $i (0..10) {
            print "thread 1\n";
            sleep (1);
        }
    }
);
my Java::Thread $thread2 = new Java::Thread(
    sub {
        for my $i (0..10) {
            print "thread 2\n";
            sleep (1);
        }
    }
);

$thread1->start();
$thread2->start();

The source code is at https://github.com/fglock/Perlito/blob/master/misc/Java/TestThread.pl; I've tested with javac 1.7.0_79 in Ubuntu.

Perl5 to Java compiler - integrating with Rhino

I'm experimenting with calling Java libraries from Perlito5-Java; calling into the JVM Javascript engine was pretty easy:

# here we import the Java packages into Perl packages:
package ScriptEngineManager {
    import => "javax.script.ScriptEngineManager"
};
package ScriptEngine {
    import => "javax.script.ScriptEngine"
};

# these are Perl variables, but they are also Java objects
# so we have to declare which package they belong to:
my ScriptEngineManager $manager = new ScriptEngineManager();
my ScriptEngine $engine = $manager->getEngineByName("JavaScript");

# now we can call the Rhino javascript engine:
$engine->eval(
    'print( "JS thinks that the result is " + ( 1+1 ) + "\n" );'
);

# JS thinks that the result is 2

The source code is at https://github.com/fglock/Perlito/blob/master/misc/Java/TestJS.pl; I've tested with javac 1.7.0_79 in Ubuntu.

Perl5 to Java compiler - 2nd hackathon

We've had another hackathon at work. This time Bosko, Bruno, Frederico, Yati, and I hacked on the Perlito Perl5-to-Java compiler.

We started adding unit tests - so that we can automatically extract a list of implemented features; the existing Perl tests are not properly organized "by feature".

The latest additions to the Java backend are: