Perlito Archives

Perl5 to Java compiler - symbol tables, typeglobs, and call stack

Perlito5 is an implementation of the Perl5 language that runs in the Java / JVM platform.

As part of the work for porting the core Perl modules, we had to implement better support for Perl symbol tables, typeglobs, and call stack.

The call stack (the caller() function) now works "natively", decoding the internal Java stack trace. This means that there is no logging overhead for calling Perl subs, and no additional work needs to be done to support Perl stack traces in JVM threads.

Symbol tables (like %::) and typeglobs also work like in Perl, even if the internal data structures are actually flattened into a java HashMap. The symbol table / typeglob / filehandle emulation doesn't add any overhead for normal hash variable access.

With these changes, the core module lib/Symbol.pm now works without modifications.

Trying out AWK in Java with Perlito5

I’ve got this example “awk” script:

$ cat x.awk 
BEGIN { print "Month Crates"
    print "----- ------" }
    { print $1, " ", $2 }

we can convert AWK to Perl with “a2p”:

$ a2p x.awk > x.pl

now create a test input file:

$ cat > x.txt
a b
d e
1 2

and try it out in Perl5-Java:

$ java -jar perlito5.jar x.pl x.txt 
Month Crates
----- ------
a     b
d     e
1     2

yay!

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.

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 - 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:

Perlito - reviewing some older code

Today I've finally updated the Perlito compiler ChangeLog, this covers a little more than a year of commits.

The most significant update was the new Perlito5-to-Java backend, which is work-in-progress. It covers a lot of ground, but there is a lot more.

There were also some interesting new Perlito5-to-Javascript features, such as regex /e modifier, and file operators support in nodejs.

The Perl6 backends have not been updated, but things still work.

While reviewing the README-perlito6 file, I've found some interesting stuff that I haven't touched in a while - here is a piece of generated code that goes through 3 different languages:

Perl5 to Java compiler is 1 month old, and we have a hackathon

We are having a hackathon at work, and Bosko, John and I have hacked together a working Perl script that executes in a Java environment (HBase).

a perl5 to Java compiler - first benchmark

I've written a small performance test. And it is not bad!

First perl:

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

real    0m3.964s
user    0m3.963s
sys     0m0.004s

And then Perl-in-Java:

$ touch Test.class ; rm Test.class ; 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.840s
user    0m0.584s
sys     0m0.302s

[EDIT] broken link

About Flávio S. Glock

user-pic I blog about Perl.