Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 796
Fernando Briano Avatar asked Mar 04 '09 16:03

Fernando Briano


3 Answers

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.

like image 181
Dave Webb Avatar answered Oct 17 '22 22:10

Dave Webb


JSON is an easy, lightweight format for passing data around.

you could add Rhino or something similar to your toolkit and may gain additional performance (not to mention a scripting engine), but this will depend on how complex you are planning your project to be.

like image 39
dsm Avatar answered Oct 17 '22 23:10

dsm


if you've got XML already, your best option is probably to continue using it.

like image 2
Tequila Jinx Avatar answered Oct 18 '22 00:10

Tequila Jinx