如何将 OO Perl 转换为 Java?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1106736/
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 convert OO Perl to Java?
提问by Bostone
I inherited large monolithic body of OO Perl code that needs to be gradually converted to Java (per client request). I know both languages but am rusty on my Perl skills. Are there any tools (Eclipse plugins?) that you folks can recommend to ease the pain?
我继承了需要逐渐转换为 Java(每个客户端请求)的 OO Perl 代码的大型单体。我知道这两种语言,但对我的 Perl 技能生疏了。是否有任何工具(Eclipse 插件?)你们可以推荐来减轻痛苦?
回答by Alexandr Ciornii
Does OO code use Moose? If yes, it is possible to convert class declarations automatically using introspection.
OO 代码是否使用 Moose?如果是,则可以使用自省自动转换类声明。
To gradually convert Perl to Java, you can include Java code into Perl program with Inline::Java.
要逐步将 Perl 转换为 Java,您可以使用Inline::Java将 Java 代码包含到 Perl 程序中。
There is Perl on JVM project, maybe it can be used to compile Perl to Java?
回答by d.weimann
The inccode.comallows you to automatically convert the perl code to java code. Nevertheless the conversion of perl variables is slightly tricky due to dynamic typing in perl. The scalar variable in perl can contain the reference to any type and the real referenced type is known when the code is executed.
该inccode.com允许你在Perl代码自动转换为Java代码。然而,由于 perl 中的动态类型,perl 变量的转换有点棘手。perl 中的标量变量可以包含对任何类型的引用,并且在执行代码时知道真正的引用类型。
Translator uses VarBox class for encapsulating all predefined types: ref(HASH), ref(ARRAY) and BoxModule for encapsulating the reference to Perl Modules.
Translator 使用 VarBox 类封装所有预定义类型:ref(HASH)、ref(ARRAY) 和 BoxModule 用于封装对 Perl 模块的引用。
The example show perl script which call two modules to print “hello world”. The module LibConsole is instantiated in script and the module LibPrinter is accessed by calling the method in LibConsole.
该示例显示了调用两个模块来打印“hello world”的 perl 脚本。LibConsole 模块在脚本中实例化,LibPrinter 模块通过调用LibConsole 中的方法访问。
#!/usr/bin/perl
use strict;
use test::LibPrinter;
use test::LibConsole;
hello_on_console( "hello world");
hello_on_printer( "hello world");
sub get_console
{
my $console = test::LibConsole->new();
return $console;
}
sub get_printer
{
#@cast(module="test::LibPrinter")
my $printer = get_console()->get_printer();
return $printer;
}
sub hello_on_console
{
my ($hello) = @_;
my $console = get_console();
$console->output ($hello);
}
sub hello_on_printer
{
my ($hello) = @_;
my $printer= get_printer();
$printer->output ($hello);
}
Translator must now the types of both modules and while perl don't define specific operators for declaring the object there's an assumption that method named “new” return the reference to module. When the method which return reference to module is named otherwise the annotation cast(module=”{class}”) can be used to inform translator about the type of the module.
翻译器现在必须知道两个模块的类型,虽然 perl 没有定义用于声明对象的特定运算符,但假设名为“new”的方法返回对模块的引用。当返回对模块的引用的方法以其他方式命名时,注释 cast(module=”{class}”) 可用于通知翻译器有关模块的类型。
The identified type of the variable will be propagate because the translator control the conformity of types in assignments.
变量的识别类型将被传播,因为翻译器控制赋值中类型的一致性。
public class hello extends CRoutineProcess implements IInProcess
{
VarBox call ()
{
hello_on_console("hello world");
return hello_on_printer("hello world");
}
BoxModule<LibConsole> get_console ()
{
BoxModule<LibConsole> varConsole = new BoxModule<LibConsole>(LibConsole.apply());
return varConsole;
}
BoxModule<test.LibPrinter> get_printer ()
{
BoxModule<LibPrinter> varPrinter = new BoxModule<LibPrinter>(get_console().getModule().get_printer());
return varPrinter;
}
VarBox hello_on_console (VarBox varHello)
{
BoxModule<LibConsole> varConsole = new BoxModule<LibConsole>(get_console());
return varConsole.getModule().output(varHello);
}
VarBox hello_on_printer (VarBox varHello)
{
BoxModule<LibPrinter> varPrinter = new BoxModule<LibPrinter>(get_printer());
return varPrinter.getModule().output(varHello);
}
}
The translated code requires runtime library to be executed.
翻译后的代码需要执行运行时库。

