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

Like embed perl interpreter in C. Is that possible? Thanks!

Replies are listed 'Best First'.
Re: How to call perl in Java?
by marto (Cardinal) on Jan 07, 2010 at 10:43 UTC
Re: How to call perl in Java?
by Ratazong (Monsignor) on Jan 07, 2010 at 10:43 UTC

    I use Runtime.getRuntime().exec() to execute my Perl-scripts from Java, redirecting the output to a file, wait till the exec() has finished and then reading in the output of my Perl-script.

    Very clumsy :-( ... but it works. ;-)

    HTH, Rata (hoping for more elegant solutions)
Re: How to call perl in Java?
by dHarry (Abbot) on Jan 07, 2010 at 14:52 UTC

    Obviously a system call does the trick as suggested by Ratazong. I don't really like the file based communication though. It's also possible to embed your Java code in Perl and make it work together like marto suggested. But at first sight PLJava doesn't look very actively supported. Another option might be Inline::Java::Callback. I recently read XML-RPC, to call perl routines from Java. I like the idea a lot. I still have to try it though.

    What kind of inter-program-communication (if any) are you looking for?

    Cheers,

    dHarry

      Not very sure, actually. Maybe I need to have java objects passed as perl sub arguments. For example:
      In foo.pm
      package foo; sub run_foo { my($obj1,$obj2) = @_; # access passed java object $obj1->method; $obj2->attribute; } 1;
      In java:
      PerlEnviromnent foo = loadPerlPackage("foo.pm"); foo.runSub('run_foo',some_object,another_object);

        Passing Java objects to Perl subs sounds tricky, objects can be arbitrary complex. Maybe you can use JSON for data transfer. You could serialize your Java object to a JSON string, pass it to Perl and decode it. See JSON in Java and JSON.

        HTH

        Harry