7

Is there any way to execute perl code without having to use Runtime.getRuntime.exec("..."); (parse in java app)?

Kryten
  • 3,843
  • 5
  • 37
  • 42

7 Answers7

5

I've been looking into this myself recently. The most promising thing I've found thus far is the Inline::Java module on CPAN. It allows calling Java from Perl but also (via some included Java classes) calling Perl from Java.

Michael Carman
  • 30,628
  • 10
  • 74
  • 122
  • 2
    are you sure about this: **can you do the reverse: calling perl from Java** , is it reliable in mission critical stuff? – FutuToad Jan 14 '14 at 14:53
4

this looks like what you're asking for

Joel
  • 16,474
  • 17
  • 72
  • 93
4

Inline::Java provides an embedded Perl interpreter in a class. You can use this to call Perl code from your Java code.

Graciliano M. Passos' PLJava also provides an embedded interpreter.

Don't use JPL (Java Perl Lingo)--the project is dead and has been removed from modern perls.

daotoad
  • 26,689
  • 7
  • 59
  • 100
2

Inline::Perl is the accepted way. But there's also Jerl which may be run from a JAR.

Here's an example without using the VM wrapper (which is not so fun).

Here's some examples using the jerlWrapper class to make it easier to code:

import jerlWrapper.perlVM;

public final class HelloWorld  {

    /* keeping it simple */
    private static String helloWorldPerl = "print 'Hello World '.$].\"\n\";";

    public static void main(String[] args) {
        perlVM helloJavaPerl = new perlVM(helloWorldPerl);  
        helloJavaPerl.run();
    }
}

or

import jerlWrapper.perlVM;

public final class TimeTest  {

    /*  The (ugly) way to retrieve time within perl, with all the
     *  extra addition to make it worth reading afterwards.
     */
    private static String testProggie = new String(
            "my ($sec, $min, $hr, $day, $mon, $year) = localtime;"+
            "printf(\"%02d/%02d/%04d %02d:%02d:%02d\n\", "+
            "       $mon, $day + 1, 1900 + $year, $hr, $min, $sec);"
    );

    public static void main(String[] args) {
        perlVM helloJavaPerl = new perlVM(testProggie);     
        boolean isSuccessful = helloJavaPerl.run();
        if (isSuccessful) {
            System.out.print(helloJavaPerl.getOutput());
        }
    }
}
j0k
  • 22,600
  • 28
  • 79
  • 90
michaelt
  • 182
  • 11
  • Jerl for Perl5 is being deprecated. Rakudo perl has VM options going forward: http://en.wikipedia.org/wiki/Rakudo_Perl_6 – michaelt Jan 06 '15 at 22:46
  • Jerl for Perl5 is indeed deprecated. While the project was stable for 2 years+, there are better virtualization options. – michaelt Jul 17 '17 at 16:31
1

I could have sworn it was easy as pie using the Java Scripting API.
But apparently it's not on the list of existing implementations...

So, maybe this helps instead : java and perl
edit: i said "maybe"

Stroboskop
  • 4,327
  • 5
  • 35
  • 52
0

No, I don't believe this exists. While there have been several languages ported to the JVM (JRuby, Jython etc) Perl is not yet one of them.

Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
0

In the future, the standard way to use any scripting language is through the java Scripting Support introduced in JSR 223. See the scripting project homepage for a list of scripting languages supported at the moment. Unfortunately, Perl isn't on there yet :-(

toolkit
  • 49,809
  • 17
  • 109
  • 135