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


In reply to Re: Perl to Java by ZZamboni
in thread Perl to Java by Eugene

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.