Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Return values to outside programs

by patt (Scribe)
on Aug 21, 2009 at 11:08 UTC ( [id://790323]=perlquestion: print w/replies, xml ) Need Help??

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

Hi fellow Monks, I am again seeking some advice. I return to a previous related post - calling Perl from a Java application. What I am seeking advice on, is how do I return results from a Perl execution to a calling application, in my case Java? I have searched the web extensively for help (several hundred sites and posts), but cannot find anything to solve my problem. Collecting return values in Java is pretty straightforward, but returning those values from Perl is causing me problems - Perl allowing rturn values only from a subroutine. One solution I was considering was writing the results from the Perl program to disk, opening an InputStream in Java and read the file written to. Is there any way I can return values directly from Perl without having to do this? How can I create an output stream to achieve this?

Replies are listed 'Best First'.
Re: Return values to outside programs
by JavaFan (Canon) on Aug 21, 2009 at 11:18 UTC
    Short answer, details about "Inter Process Communication" is found in perlipc.

    Longer answer: it depends what you want to communicate, and at which time. Signalling a single bit can be done by sending a signal. You have 8 bits of communication on program exit: the exit value of your program can be waited on by the parent process. Parent and child can share a pipe or a socket, and communicate via the pipe. Two (or more) processes can use shared memory to exchange messages. If messages are important, and should not be lost even if one of the processes goes belly up, you may want to use a message queue. Perhaps you want to piggyback on an existing frame work to exchange information in the form of a server/client relation: HTTP, FTP, etc, or something that's implemented on top of such a protocol, like SOAP. Or if the data needs to be persistent, you might want to use a database.

    I'm sure that for whatever method you pick, you'll be able to find both Perl and Java modules doing the grunt work.

      That is an excellent reply, awesome expert advice, ++JavaFan

      this community has absolute (unconditional) sense of sharing.... LONG LIVE PERLMONKS......


      Vivek
      -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
Re: Return values to outside programs
by Bloodnok (Vicar) on Aug 21, 2009 at 11:19 UTC
    It depends on the value(s) you're attempting to return from the perl script - for simple integer values exit is your friend - for more complicated return values, something along the lines you're already following sounds like the way since you've already got it working.

    A user level that continues to overstate my experience :-))
      Thanks for those replies. My return values are complicated, being String, integer, octal and boolean (either singly or combined). I was looking at pipes just after I posted, but they don't appear to solve my problem. I'll investigate perlipc (have not done that yet, was not aware of it). Since my returns may be only one line of 'text' or several hundred thousand, depending on the search criteria entered I'm strongly leaning towards write to disk, read from disk as the most efficient way to solve my problem.

        You could use data serialization with XML. That way you can get the data back as a Java object. The Perl program just need to write the results somewhere as a file.

        If this is too simple for you application (like it receives several concurrent requests) then you should look for using sockets and XML.

        Alceu Rodrigues de Freitas Junior
        ---------------------------------
        "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

        If you have large, complex data sets, you might consider a database as an intermediary rather than serialization to a file.

Re: Return values to outside programs
by rovf (Priest) on Aug 21, 2009 at 11:32 UTC
    If you have more data to return than an 'exit' would do, you could use named pipes (the caller creates the pipe and passes the name to the Perl program).

    Still another possibility - provided that your Perl program does not use them otherwise - would be to have Perl output the return data to STDOUT or STDERR. I'm not so proficient with Java, but I guess there will be something equivalent to Perl's qx() in Java too...

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Return values to outside programs
by Anonymous Monk on Aug 21, 2009 at 15:09 UTC
Re: Return values to outside programs
by vishi83 (Pilgrim) on Aug 21, 2009 at 13:09 UTC

    You can read about modules like Java, Inline::Java and Java::Import

    Thanks..
    Vishy


    A perl Script without 'strict' is like a House without Roof; Both are not Safe;
      I appreciate all your help, thanks.

      I'm going to try piping my data to Perl and return it via pipes. Never tried it so this will be a learning experience.

      The more I learn about Perl, the more I am impressed by it. Simple, quick to write, easy to use.

      Thank you all...
      I know I'm off point, but I did look at Tk, NetBeans is better than it. The others I haven't tried. I'm used to N/beans, and it is my philosophy if it ain't broke, don't fix it. As it is many others way of thinking, I'm sure.

      <\p>Just to finish this discussion I have Perl performing perfectly. It got over it's little hump and started talking to Java, or maybe Java was the moody one, or maybe I was the problem.

      <\p>Thanks for all the help folks, I do appreciate it. The solution turned out to be very simple. It was staring at me, and if it had legs it would have kicked me...

Re: Return values to outside programs
by fitzcarraldo (Sexton) on Aug 21, 2009 at 20:44 UTC
    Maybe you want to consider embedding Perl in your Java application. I've never done that before, so I can't give any further help.
    Here is what I found on that topic. Hope it helps.
    http://www.perlmonks.org/index.pl?node_id=373839
    http://nbperl.blogspot.com/2009/02/different-ways-to-use-perl-cpan-modules.html
      I have considered embedding it in Java, but just looked at it once. I simply wanted the best of both languages - Java for the pretty stuff and Perl for the grunt stuff. I have plenty of good ideas to work on now, and again thanks all.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found