如何从 Java 调用 Perl? [英] How can I call Perl from Java?

查看:45
本文介绍了如何从 Java 调用 Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 模块,我想从 Java 中使用它.有没有办法使用 Windows 上的 ActiveState Perl 或 Linux 附带的通用 Perl 来调用此代码?我找到了对 JPL 的引用,但它似乎不再被维护.

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.

推荐答案

Inline-Java 是从 Perl 调用 java 的常用库,这个 post 提出了一个 org.perl.java 模块,它应该允许从 Java 调用 Perl,如所问.

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.

然而,由于不同的 JNI 实现的不可预测性JVM 很难说 JVM 和 Perl 的哪些组合会起作用.通常,所需的是具有 MULTIPLICITY 的 Perl,并编译了线程.这意味着他使用自定义构建的 Perl.

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.

否则,Inline::Java::Callback 允许您从 Java 调用 Perl 函数.为此,您需要创建一个 org.perl.inline.java.InlinePerlCaller 对象.以下是典型用途的示例:

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/) ;
} 

这篇关于如何从 Java 调用 Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆