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

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

问题描述

我有一个想从Java使用的Perl模块.是否可以使用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的常用库,该

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天全站免登陆