Search code examples
javaperllanguage-interoperability

How can I pass data from Perl to Java?


I'm working on some Java <-> Perl interaction. I would like to know what the best way is to pass information from Perl to Java. (Great answers about Perl and Java here and here btw).

There's a lot of text and XML(XML::Twig) I'm parsing in Perl, in a script I'm supposed to call from a Java Web App. So I have all this gathered data, and I need it to use it inside certain objects in Java.

What could be a good strategy to send the information from Perl to Java? Is it even possible to return an object or other compatible data structure from Perl to Java?

I guess writing to a text file and reading it from Java would make all the optimization gained by using Perl meaningless.

Perlformance is an important issue here.

EDIT: From what I've seen here, maybe Inline-Java would be a good option?


Solution

  • If performance is important I'd recommend having a persistent Perl process running. Starting a Perl interpreter every time you want to run your code will be quite an overhead.

    The easiest way to communicate is for the Java process to open a TCP connection to the Perl processs, write some data and get some back.

    The format you use to send data to from your Perl process back to your Java one will depend on what you're sending and how generic and re-usable you want your code to be. Sending back XML strings will be nice and generic but sending back byte arrays created with pack in Perl and then read a with DataInputStream in Java will be much, much faster.