Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Perl to Java

by Eugene (Scribe)
on May 10, 2000 at 22:19 UTC ( [id://11021]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a module or anything else that would allow my Perl program talk to java and java to Perl?

Replies are listed 'Best First'.
Re: Perl to Java
by ZZamboni (Curate) on May 10, 2000 at 23:24 UTC
    As btrott said, unless you are using some very specific preexisting class in your Java program, I don't think there is anything you could not do in Perl with respect to networking. So why not do the whole thing in Perl?

    But if you really need to split it between the two, one simple way of passing things between a Java and a Perl program would be to use pipes. Assuming you are on Unix, that is.

    If the perl program gets executed first, you could create a couple of pipes, fork a child process and execute the Java program there. Then the Java program can read from STDIN and write to STDOUT to communicate with your perl program. Something like this:

    pipe RDRPARENT, WRTCHILD or die "Error: $!\n"; pipe RDRCHILD, WRTPARENT or die "Error: $!\n"; if (defined($pid=fork)) { if ($pid) { # In the parent # Close child's ends of the pipes close(WRTCHILD); close(RDRCHILD); # Continue, reading from RDRPARENT and writing to # WRTPARENT to communicate with the Java program. } else { # In the child # Close parent's ends of the pipe close(WRTPARENT); close(RDRPARENT); # Redirect STDOUT and STDIN to the pipes. open(STDOUT, ">&WRTCHILD") or die "Error: $!\n"; open(STDIN, "<&RDRCHILD") or die "Error: $!\n"; # Execute the java program exec("java prog.class"); die "Exec error: $!\n"; } } else { die "Fork error: $!\n"; }
    Something similar could be done if the Java program is the one that gets executed first, but my Java is too rusty to make an attempt at that.

    --ZZamboni

      if you don't want to cut and paste all that code:
      perldoc IPC::Open2
      Unless you are paid by the line for code. :)
        Even if you are paid by the line, why not copy and paste? Get a hefty bonus.
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl to Java
by btrott (Parson) on May 10, 2000 at 22:28 UTC
    Talk in what sense? You could talk using sockets. You could write client-server software--perhaps the server in Perl, the client in Java, or the other way round--open up a socket connection, then talk through the socket.

    Does that fit with what you're trying to do? If not, could you give some more details?

Re: Perl to Java
by Eugene (Scribe) on May 10, 2000 at 22:49 UTC
    I am writing a program that is similar to FTP client. On the server side I grab the stuff that is being submitted, and process it. I gonna use Java to do the networkish things, but would like to use as little Java as possible, and write most of the program in Perl. So I need a way to pass things to and from Java classes.

    I just found something called JPL, but there is not much on how to use it and where I can get it.
      Alligator Descartes wrote an article called Java and Perl Harnessed (JAPH for short :o)).

      Apparently he has been working (sucessfully) on a model to embed perl into java. The article was presented to LW back in 1997's perl conf, so I don't know what evolved since then. It may be just theoretics, but I suppose this may also be what you are looking for.

      Since I am also java-dumb, I can only recommend on the basis that I found something, but not necessarily that I know what it means, or how to use it. HTH!

      BTW: Check out the guy's bio for fun, he looks like a mean mutha!
      Apparently JPL started shipping with the Perl distribution as of 5.005_54 or somesuch release (according to this press release). So if you have a recent release, you may have the JPL stuff. Take a look at the results on search.cpan.org.

      As for how to use it... well, I've really no idea. :)

      Why do you want to use Java for your network code, incidentally? Rather than Perl's socket support, I mean.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-29 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found