• Post Reply Bookmark Topic Watch Topic
  • New Topic

java calling perl  RSS feed

 
tarun Mehra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone ever come up with a way to call a Perl program from Java? If so, how are input and output handled?
Thanks,
Tarun
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the standard way is with the Runtime exec() method. I don't know a platform-neutral way to do this, but usually the only thing that changes from system to system is the path to the perl execuatble - so make this user-configurable (like with a properties file) and this isn't too big a deal. For communications you have a number of options - exec() returns a Process, which has getInputStream(), getOutputStream(), and the not-to-be-forgotten getErrorStream() method, which allow communication with the process (your perl script with perceive these as its standard input, standard output, and standard error, respectively. But these are oftern a pain to work with - it's often easier to communicate through files. Put the data you want processed in one file, and have the perl script output its results in another file. Assuming that sort of arrangement makes sense for what you're doing. I assume perl can also communicate through sockets, so that's also an option if you prefer.
Lastly, if you're not too committed to perl already, you might want to look at the Python programming language, which is perhaps perl's biggest rival among interpreted languages. It's much more readable, and there's a Java implementation known as Jython, which is very easy to interface with in a platform-neutral manner. Check out www.python.org and www.jython.org. Enjoy...
 
mike warner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about a slightly less complex & perhaps more universally needed solution...
A java servlet passing CGI variables to a perl script for the perl script to consume & vice versa. How would one do that particularly when the perl script is likely running under a different port ( eg, 80) & the servlet ( eg Tomcat ) is running under 8080???
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, it depends what you are using perl for? If its just for the powerful regular expressions, then Java has implemented simlar regular expression support in Java 1.4:
http://java.sun.com/j2se/1.4/docs/api/java/util/regex/package-summary.html
If you are stuck with an earlier version of Java, then Apache have a open source version you could try:
http://jakarta.apache.org/regexp/index.html
Have fun
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!