Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Calling perl from java

by fletcher_the_dog (Friar)
on Oct 15, 2003 at 19:19 UTC ( [id://299506]=perlquestion: print w/replies, xml ) Need Help??

fletcher_the_dog has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am working with an application that is all in java. At certain points in this application there are places you can plugin scripts or executables to do some processing of potentially 10,000's of files. In the original version of this application these scripts were called in a 'batch' mode and where given a list of all the files they needed to run over. Now the architecture of the application has changed so that the scripts run over just one file at a time. This creates a massive overhead, because the application has to shell out to perl for everyfile. The creators of the application said to get rid of the overhead, scripts should be coverted to java classes and the classes can be called directly from the program and not have to shell out to the system. Well this creates quite a problem for me. I either have to deal with the high overhead or try to convert some fairly complex, regex heavy scripts to java. So I was wondering if there is a way to call perl functions from java.

Replies are listed 'Best First'.
Re: Calling perl from java
by ptkdb (Monk) on Oct 15, 2003 at 19:58 UTC
    The tool to call perl from java would be JNI(Java Native Interface). Which is a rather rich API provided by Sun. The definative reference/tutorial is Java Native Interface.

    It can be tricky, but it can be done.

    HOWEVER Given that you've implied that most of the need to call perl from java lies in Perl regexe's you should be aware that as of Java 1.4.1, Java includes a 'Pattern' class whose syntax closely parallels the Perl regex syntax with some differences(ref Pattern Docs)

    It pains me to recommend this, but you would probably do well to examine how hard it would be to convert your existing perl regexes to Java Patterns if Java 1.4.1 is in use. If the patterns are relatively simple and you get the hang of Java patterns quickly enough, then you should probably go with converting your perl regexes to Java Patterns. Two days of kicking the tires on trying Java patterns to see if it would work would probably be time very well spent. If you're using regex syntaxes that are not well supported in Java patterns, look again at the JNI solution.

    Also consider that if you move to a different platform, you're going to have to make sure that your JNI library works on that other platform as well.

    Keep also in mind the people who follow you on this project. If you manage to keep it all Java, you've simplified their lives considerably. If you go the JNI route, they'll have to support Java, Perl, C/C++, Makefiles and JNI's own collection of issues.

    Step back, and examine the pros and cons of both approaches very carefully.

      Given the highly OO nature of Java's new regex classes, they take far more lines of code to do the same thing as a Perl regex (not to mention that ?{ } isn't quite Perl Compatible :). Calling the Perl code without shelling-out will almost certainly be easier than dealing with Pattern.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      :(){ :|:&};:

      Note: All code is untested, unless otherwise stated

        As a longtime user and lover of Perl, I have to disagree with your assessment of Java's regex support. While it may be true that dealing with the Pattern object is a real pain, in the 1.4 JDKs they have also added to the String object .matches(), .split(), and other handy methods for dealing with regular expressions. Though you may still have your qualms about writing this type of code in Java (I for one abhor the hoops one has to jump through to get certain metacharacters properly quoted and escaped), it's not quite as bad as you make it seem. :-)

        Update: oooh, downvotes! Apparently the monks don't like posts that are anything less than completely critical of Java, even when they are correcting an error of misunderstanding, and point out something else bad about Java. Very nice. Sorry for the whining. 8^\

Re: Calling perl from java
by agentv (Friar) on Oct 15, 2003 at 20:19 UTC
    ...here's another possibility. Certainly JNI is an excellent way to call native functions (such as those in the Perl libraries) directly from a class file, but I will bet that your scripts don't convert easily into something that would work this way.

    And what would be easiest varies from programmer to programmer. (When someone tells me what "would just be the easiest" I translate that to mean "if I had to do the job, it would be easiest to...")

    And I'm willing to bet that the use of regexes in your existing scripts will not translate well to Java Patterns due to the many nuances of regex implementation in Perl and our inability to avoid using those nuances over and over again.

    So what I was thinking would do the trick is to build a small network server in Perl that is launched at the same time as your application and which answers on a port known to your application. Then, when the time comes to call for a file operation, the Java class you provide will actually send a request to your already waiting Perl server which would process the job.

    So if you find out from your application architects how to supply a Java class that will service the request, then write such a class that forwards the request to your Perl server. The server could be very simple, just listen on a designated port, then receive a formatted request to service a file, do that job and then go back to listening.

    This would result in the elimination of the overhead caused by process creation. The Perl server could be simple enough to fit in about two printed pages of code, and it could provide the gateway to the rest of your scripts.

    And you'd learn a great deal about both Perl and Java as you write it!

    ...All the world looks like -well- all the world, when your hammer is Perl.
    ---v

Re: Calling perl from java
by pg (Canon) on Oct 15, 2003 at 19:52 UTC

    Yes, and just do it thru JNI (Java Native Interface), and you can find the spec on Sun’s web site. You can view it as a group of functions that allow you to access code written in native language from Java.

    The way Abigail-II pointed out works, and I have this kind of stuff running in production. It is quite stable, and amazingly, it is quite fast.

Re: Calling perl from java
by Abigail-II (Bishop) on Oct 15, 2003 at 19:39 UTC
    o I was wondering if there is a way to call perl functions from java.

    Well, that would be Java question, wouldn't?

    The answer is "yes", but unless someone else did it before (and I'm not aware someone did, but I don't do Java), it's not going to be easy. The short answer is, Perl is embeddable in C programs, and there are Java compilers/runtime environments written in C. So, it's possible. It's probably a lot easier to convert your Perl to Java.

    Another approach would be to point out that before there was no overhead, and the overhead is created by a change (for the worse) of the design. Hence, the design is flawed, and it's the design that needs to be fixed.

    Abigail

      ++; Also it may be possible to write a very low overhead C program or shell script that just takes the input and writes it to a queue file that your script can process later.

      I agree though that their change has created more overhead.


      -Waswas

        That'll get rid of some overhead, but it is likely that the massive ammounts of shelling-out itself is the major source of overhead, not running the external program itself. Although benchmarks would have to be done to be sure, I doubt such a C program would be fast enough.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        :(){ :|:&};:

        Note: All code is untested, unless otherwise stated

Re: Calling perl from java
by Arunbear (Prior) on Oct 15, 2003 at 20:42 UTC
    Hello, have you considered Jython? This allows you to call Python from Java or Java from Python.

    Admittedly, Python's string handling is not as nice as Perl's (regexps are not built into the Python core language - you have to use the re module), but it will still be a hell of a lot simpler than a Java only solution.

    Python is very easy to pick up, especially if you already know Perl. Good luck!
Re: Calling perl from java
by Beechbone (Friar) on Oct 16, 2003 at 11:59 UTC
    JNI is the right answer, and with the little more info that the module JPL (in Perl core since 5.8.0, or from CPAN) will* convert your Perl+Java to a JNI lib and the matching Java, it may also be useful.

    *) For some platforms at least...


    Search, Ask, Know

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://299506]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-19 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found