http://qs1969.pair.com?node_id=666905

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

What options do I have in terms of using Perl modules in other languages, or vice versa? What should I search for on CPAN? And finally, is there a list or a Perl project documenting how to do this?

Replies are listed 'Best First'.
Re: Interfacing Perl with other languages
by Corion (Patriarch) on Feb 08, 2008 at 06:55 UTC

    Being able to use Perl modules is fairly unlikely, because most languages are not like Perl. You will likely be able to use (80% of) Perl modules with Kurila, a Perl fork. Perl6 (if it ever comes out) is supposed to have a Perl5 interpreter mode, but as far as I know, currenly only the Perl5 interpreted version of Perl6 is able to run Perl5 code (no surprise).

    You can embed a Perl interpreter into any language that can embed C subroutines - see perlembed for how to do this. You will need a C compiler though, to compile Perl for that.

    You can use other languages from Perl, by using the Inline family of modules. Most of these modules require a compiler for the other language.

    There are some ways of using parts written in other languages that are specific to different operating systems. On Windows, you can transparently use any object that has an OLE interface through Win32::OLE. To write an OLE object in Perl, you have to buy the ActiveState PerlKit (I believe), but that works on Windows only. For OSX (and I believe MacOS as well), there are AppleScript events that you can issue from Perl. I'm not sure if the CamelBones library supports receiving such events as well.

    The most generic ways of interfacing two programs is to either run the other code as a subprogram (see perlipc) or have one program be a webserver and use the other one to make requests to it.

Re: Interfacing Perl with other languages
by salva (Canon) on Feb 08, 2008 at 10:24 UTC
    You have to write an interface between the two languages, and for that there are two tipical ways:
    • Using a socket: both languages run in their own process space and some kind of RPC is used to call one into the other through the socket. You have to serialize the data crossing the interface and deserialize at the other side while remaping it to the other language types. Using something standard as XML or YAML for the serialization is an option, but it has a big overhead.
    • Using XS (or Inline, SWIG, etc.): both languages run in the same process and the data is converted between the two languages using some C code. You have to provide a set of functions on every side of the interface to call the other language (for instance, on the language calling perl: perl_call_sub, perl_call_method, perl_eval). That solution is usually much faster, but you have to program it in C/XS.

    If you tell us what is your target language, we would be able to give more specific indications about how it can be done.

      Thank you all for the fantastic replies. I thought Inline was a very interesting bit of Perl. I also appreciate the pointers towards perl4caml and CamelBones.

      The question came out so vague because I was simply curious about the issue of how various languages played nice with one another. Solutions that I already knew about include setting up XML-RPC, SOAP, or some other interface that's interoperable between languages. I know almost nothing about XS, but I think that's the solution I was more interested in.

      I originally thought of the question when I was going through some particularly nasty legacy perl code. I thought the code was perfect for a basic CRUD webapp in Rails. However, since there were many other legacy systems that depended on the legacy code, I wanted to make a Ruby interface to the legacy code, and use that Ruby interface in the webapp. Wrapping around the legacy code would allow me to design away all the years of hacks and be able to test it at the same time.

Re: Interfacing Perl with other languages
by jrtayloriv (Pilgrim) on Feb 08, 2008 at 09:31 UTC
    I haven't used Perl from many other languages, however one where I have is from PHP, with their perl package

    There are several modules on available on CPAN for interfacing with other languages -- Prolog, Scheme (via the Guile Interpreter), Inline::C, Inline::Python ...the list goes on. You've asked a fairly broad question. What languages are you interested in? Perhaps being more specific would help people give a better answer...

    As far as searching on CPAN, why not just put in the name of the other language you are working with, and see what pops up?
Re: Interfacing Perl with other languages
by apotheon (Deacon) on Feb 08, 2008 at 09:50 UTC

    You can use Perl modules in OCaml programs with perl4caml.

    print substr("Just another Perl hacker", 0, -2);
    - apotheon
    CopyWrite Chad Perrin

Re: Interfacing Perl with other languages
by Erez (Priest) on Feb 08, 2008 at 13:47 UTC

    CPAN has a separate category for Language interface that suggests some ways of interfacing with other languages. There are also different inter-operability approaches ranging from Web-Services to language-specific Parsers. For specific types of interfacing/languages, there's always Google.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphores.

Re: Interfacing Perl with other languages
by dynamo (Chaplain) on Feb 08, 2008 at 18:19 UTC
    I have found one particular interface very useful for interfacing with ObjectiveC (for Mac programming) - CamelBones.

    If you are into ObjectiveC, and perl, it's a lot of fun..

    The other one that comes to mind, that I hadn't already seen here may be too obvious to mention, but it's very useful:
    backticks. You can pass arguments via shell and files pretty intuitively.

    -d

Re: Interfacing Perl with other languages
by peterdragon (Beadle) on Feb 09, 2008 at 22:20 UTC
    Look on CPAN under Inline for lang-under-Perl. You can do it the other way too though you don't say which language you're interested in, e.g. PHP and PHP::Session.

    Regards, Peter