Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Perl in Java

Tags:

java

include

perl

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

like image 375
Kryten Avatar asked Jun 09 '09 16:06

Kryten


4 Answers

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.

like image 89
Michael Carman Avatar answered Nov 08 '22 14:11

Michael Carman


this looks like what you're asking for

like image 34
Joel Avatar answered Nov 08 '22 13:11

Joel


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.

like image 4
daotoad Avatar answered Nov 08 '22 13:11

daotoad


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());
        }
    }
}
like image 2
michaelt Avatar answered Nov 08 '22 14:11

michaelt