如何从 Java 调用 Perl?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I call Perl from Java?
提问by Jared
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.
我有一个 Perl 模块,我想从 Java 中使用它。有没有办法使用 Windows 上的 ActiveState Perl 或 Linux 附带的通用 Perl 来调用此代码?我找到了对 JPL 的引用,但它似乎不再被维护。
采纳答案by VonC
Inline-Javais the usual library to call java from Perl, and this post propose a org.perl.javamodule which should allow calling Perl from Java, as asked.
Inline-Java是从 Perl 调用 java 的常用库,这篇文章提出了一个org.perl.java模块,它应该允许从 Java 调用 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.
然而,由于不同 JVM 的 JNI 实现的不可预测性,很难说 JVM 和 Perl 的哪些组合将起作用。通常,需要的是带有 MULTIPLICITY 的 Perl 和编译的线程。这意味着他使用自定义构建的 Perl。
Otherwise, Inline::Java::Callbackallows 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:
否则,Inline::Java::Callback允许您从 Java 调用 Perl 函数。为此,您需要创建一个org.perl.inline.java.InlinePerlCaller
对象。以下是典型用途的示例:
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/) ;
}
回答by Geo
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.
我不知道它有多稳定,或者它维护得有多好,因此,另一种选择是编写一个脚本来执行您的应用程序需要的操作,然后从 Java 执行该脚本。不是最优雅的方式,但它有效。
回答by Olie
Is this not what Runtime.exec() is for?
这不是 Runtime.exec() 的用途吗?
Runtime.getRuntime().exec("/usr/bin/perl myPerl.pl");
Or am I misunderstanding the question?
还是我误解了这个问题?
回答by Matthew Watson
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.
我曾经使用过 Inline::Java,发现它有点繁琐,如果我有时间的话,我可能会重新实现使用 Web 服务并以这种方式调用 perl 代码。
回答by Matthew Watson
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
我在 JavaWorld 上找到了 Robert Lawson 的实现,它使用 XML-RPC 从 Java 代码调用 Perl 例程:Call Perlroutines from Java
回答by Sarel Botha
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.
Sleep 是一种脚本语言,带有运行在 JVM 中的解释器。据我了解,睡眠语言基本上是带有一些扩展的 Perl。您的代码可能能够在睡眠模式下运行。如果是,您可以实例化解释器,运行代码并检索结果。
回答by wulfgarpro
回答by ?yvind Skaar
回答by sventechie
Rakudoallows you to run Perl6 inside a JVM, and there is a Perl5 add-onthat allows you to run most old code, although no XS of course. There is also JERLthat runs current microperl inside the JVM. It depends a lot on what you're looking to do, but those are worth checking out.
Rakudo允许您在 JVM 中运行 Perl6,并且有一个Perl5 附加组件允许您运行大多数旧代码,当然没有 XS。还有JERL在 JVM 中运行当前的microperl。这在很大程度上取决于您要做什么,但这些都值得一试。