How can I convert OO Perl to Java?

前端 未结 3 643
滥情空心
滥情空心 2021-01-20 16:35

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. Ar

相关标签:
3条回答
  • 2021-01-20 17:12

    Does OO code use Moose? If yes, it is possible to convert class declarations automatically using introspection.

    To gradually convert Perl to Java, you can include Java code into Perl program with Inline::Java.

    There is Perl on JVM project, maybe it can be used to compile Perl to Java?

    0 讨论(0)
  • 2021-01-20 17:27

    I'd say PLEAC is one of the greatest resources.

    0 讨论(0)
  • 2021-01-20 17:31

    The inccode.com allows 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.

    Translator uses VarBox class for encapsulating all predefined types: ref(HASH), ref(ARRAY) and BoxModule for encapsulating the reference to Perl Modules.

    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.

        #!/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.

    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.

    0 讨论(0)
提交回复
热议问题