How can I call Perl from Java?
Asked Answered
S

9

16

I have a Perl module that I would like to use from Java. Is there a way to call this code using either ActiveState Perl on Windows or the generic Perl that comes with Linux? I have found references to JPL but it doesn’t appear to be maintained anymore.

Sublieutenant answered 8/11, 2008 at 15:44 Comment(1)
is there a need to pass data around from perl back to java? that is usually where problems/difficulties come in, otherwise, it'd be a matter of opening a process and executing perl script.Coquetry
J
18

Inline-Java is the usual library to call java from Perl, and this post propose a org.perl.java module which should allow calling Perl from Java, as asked.

However, because of the unpredictability of the JNI implementations for different JVMs it is difficult to say what combinations of JVM and Perl will work. Typically, what is required is Perl with MULTIPLICITY, and threads compiled in. That means he uses a custom built Perl.

Otherwise, Inline::Java::Callback allows you to call Perl functions from Java. To do this you need to create an org.perl.inline.java.InlinePerlCaller object. Here is a example of a typical use:

use Inline Java => <<END ;
import java.util.* ;
import org.perl.inline.java.* ;

class Pod_regexp extends InlineJavaPerlCaller {
    public Pod_regexp() throws InlineJavaException {
    }

    public boolean match(String target, String pattern)
        throws InlineJavaException {
        try {
            String m = (String)CallPerlSub("main::regexp",
            new Object [] {target, pattern}) ;

            if (m.equals("1")){
            return true ;
        }
    }
    catch (InlineJavaPerlException pe){
        // $@ is in pe.GetObject()
    }

    return false ;
    }
}
END

my $re = new Pod_regexp() ;
my $match = $re->match("Inline::Java", "^Inline") ;
print($match . "n") ; # prints 1

sub regexp {
    my $target = shift ;
    my $pattern = shift ;

    return ($target =~ /$pattern/) ;
} 
Jordanson answered 8/11, 2008 at 15:52 Comment(0)
B
7

Is this not what Runtime.exec() is for?

Runtime.getRuntime().exec("/usr/bin/perl myPerl.pl");

Or am I misunderstanding the question?

Baillargeon answered 8/11, 2008 at 16:19 Comment(0)
R
3

Rakudo allows you to run Perl6 inside a JVM, and there is a Perl5 add-on that allows you to run most old code, although no XS of course. There is also JERL that runs current microperl inside the JVM. It depends a lot on what you're looking to do, but those are worth checking out.

Riga answered 30/7, 2013 at 19:53 Comment(0)
V
2

I've used Inline::Java a bit, and found it a bit fiddly, if I had my time over, I'd probably reimplement using web services and call the perl code that way.

Vegetative answered 8/11, 2008 at 23:13 Comment(0)
G
2

Sleep is a scripting language with an interpreter that runs in the JVM. From what I understand the Sleep language is basically Perl with some extensions. Your code may be able to run in Sleep. If it does you can instantiate the interpreter, run the code and retrieve the result.

Garold answered 7/6, 2010 at 13:1 Comment(1)
Sleep cannot run any interesting Perl code due to its incompatibility. You cannot substitute Sleep for Perl.Snafu
G
0

I found an implementation on JavaWorld by Robert Lawson, which uses XML-RPC to call Perl routines from your Java code: Call Perl routines from Java

Gethsemane answered 11/11, 2008 at 12:0 Comment(0)
C
0

I know this is old, but I recently came across the same needs. I found JPerl more convenient then Inline::Java::PerlInterpreter.

Convenience answered 26/7, 2011 at 1:43 Comment(0)
U
0

Guess it really depends on what your perl code is, and what you're trying to do..

If just using exec() is too simple, something like gearman could be helpful

Untoward answered 26/7, 2011 at 9:44 Comment(0)
N
-1

I don't know how stable will that be ,or how well maintained it is, so, another option would be to write a script that does something your application will need and then execute that script from Java. Not the most elegant way, but it works.

Nondescript answered 8/11, 2008 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.