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.

Leave a comment

About Flávio S. Glock

user-pic I blog about Perl.