Perl5 as a first-class script engine in Java - part 2

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

jrunscript now works with the "Perl5" language parameter:

$ jrunscript -cp .:perlito5.jar -l Perl5
perl> push @INC, "src5/lib"
1
perl> use Env qw/ HOME /;

perl> say $HOME
/Users/fglock
1
perl>

Perl5 as a first-class script engine in Java

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

Perl scripts can now be run from inside Java applications using the standard javax.script API:

// $ javac -cp .:perlito5.jar Script.java
// $ java -cp .:perlito5.jar Script

import javax.script.*;

public class Script {
    public static void main(String[] args) throws Exception {

        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("Perl5");

        Object o = engine.eval(" $x = 456; say 123 + $x; \"value was $x\" ");
        System.out.println("result: " + o);
    }
}

Perl modules can be loaded after setting @INC.

About 40 core modules have been ported to this distribution, and it passes about 23,000 tests.

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