5

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?

Community
  • 1
  • 1
Fernando Briano
  • 7,579
  • 13
  • 55
  • 74
  • so are you trying to make your old java code into perl? – TStamper Mar 04 '09 at 16:52
  • 1. try JSON 2. get rid of perl, and use Java only :) – Vladimir Dyuzhev Mar 04 '09 at 20:15
  • I was reading through the post of that question and it was recommended to use JSON, Inline-Java, open TCP Connection and transfer data and lastly directly pass XML. * Which one is better amongst suggested approaches ? * Are there are other better options to do so, what about Web Services or any other suggestion ? – Rachel Oct 19 '09 at 21:15

5 Answers5

5

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.

Dave Webb
  • 179,733
  • 56
  • 298
  • 296
  • You can actually use Inline::Java this way. It can create a jvm process that communicates with your Perl process. It's a custom format or course, but it let's you use native Java objects in Perl and vice-versa – mpeters Mar 04 '09 at 18:57
3

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.

dsm
  • 9,895
  • 35
  • 70
2

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

Tequila Jinx
  • 438
  • 1
  • 3
  • 8
  • Our first implementation had Java parse the XML's, but we found Perl is much faster and more effective in doing this. So we are trying to derivate that job to Perl which is better at it. – Fernando Briano Mar 04 '09 at 16:46
  • You're probably doing something very strange if Perl is parsing XML faster than Java. – Tom Hawtin - tackline Mar 04 '09 at 16:50
  • We were using xPath to navigate XML files and obtaining node values; but, isn't Perl oriented to that kind of work (text parsing and stuff like that)?? – Juan Manuel Mar 04 '09 at 16:53
  • 1
    I'd ignore Tom Hawtin's thought that somehow Java will beat Perl at XML parsing. When it comes to parsing text, Perl is king - http://www.tbray.org/ongoing/When/200x/2007/10/30/WF-Results – mpeters Mar 04 '09 at 18:52
  • 2
    The reason Perl might be faster than Java at parsing XML (I wouldn't know for sure) is that Perl doesn't parse XML, it uses standard C libraries to do the parsing (expat or libxml2) and modules to access those libraries. – runrig Mar 04 '09 at 19:31
2

Inline::Java. It works really well once you get it up and running. I was working on a project a few years ago that had a Perl web application talking to a Java SOAP server for some realtime communication and it was just way too slow. We replaced it with a system using Inline::Java for the communication and it was much faster. Definitely minimize the points that you pass objects back and forth and try to make those objects simple (we stuck with strings, numbers and arrays) to keep things from getting too out of control. But I'm definitely a convert!

mpeters
  • 4,332
  • 2
  • 25
  • 38
0

I also find it strange that your perl code for parsing XML would be significantly faster than the equivalent code in java. Even if it is, though, I can't imagine that it would ever be faster than the overhead incurred by the IPC. Even if you use a persistent perl process, you're still going to have to send it data, presumably over a socket, then get some data back, and finally deserialize it into something usable.

Have you tried improving the performance of your XML parsing in java? If you're using DOM or a third-party library like JDOM or castor, try using SAX instead. Or maybe just using regular expressions instead of parsing the XML would be faster (jwz notwithstanding).

In any case, I would recommend using a profiler with your java code first to see if it can be improved.

Jason Day
  • 8,629
  • 1
  • 38
  • 46
  • 1
    There is a pure perl XML parsing library, but nobody who cares about speed uses it. Most perl XML parsing involves expat or libxml2 under the hood which is fast. – runrig Mar 04 '09 at 19:35
  • I don't see how that's going to make much difference. Even if the raw XML parsing is significantly faster, you've still got the IPC overhead. – Jason Day Mar 05 '09 at 17:32