Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Speaking between Perl and C

by ReinhardE (Sexton)
on Jun 02, 2005 at 14:09 UTC ( #462920=perlquestion: print w/replies, xml ) Need Help??

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

I was working on embedding Perl scripts in a software written in C. First thing what one would try out is the Perl C Interface. This approach however has also some disadvantages ( converting structures to objects, C variables to Perl variables, fiddling with the Perl stack, ......)
Another idea would be a "loosely coupled system". A process( written in Perl ) and listening on a TCP port. The software in C wishing to "use" the Perl scripts than contacts simply the process written in Perl and passes the data the Perl script act on. This should work and is also very transparent.
Another idea would be RPC. Now is there somebody out there who just tried out Remote Procedure Calls between Perl and C ? Or even better : did somebody already use shared memory between C and Perl ?
Are my dreams so far from reality ?
Big thanx for any comment

Replies are listed 'Best First'.
Re: Speaking between Perl and C
by salva (Canon) on Jun 02, 2005 at 15:22 UTC
    If efficiency is not much of an issue for you, using SOAP is a possibility: gSOAP on the C side and some SOAP module from CPAN on the Perl side (for instance, SOAP::Lite).

    Anyway, for a good C programmer, learning XS is not so hard, after a couple of weeks, you will feel confortable using it.

Re: Speaking between Perl and C
by kutsu (Priest) on Jun 02, 2005 at 14:14 UTC

    Take a look at Inline::C :)

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      Yes I know there are modules available which help much.
      What I however was looking for was really to separate the two worlds, Perl and C.
      Letting them comunicate between each other . . .

      This would be only the first step. The second would be to have more than one Perl process doing the actual work of data retrieving. The Perl backend should work as repository.

Re: Speaking between Perl and C
by Steve_p (Priest) on Jun 03, 2005 at 01:40 UTC

    I feel like I said this a bunch of times here recently, but you should take a look at POE. Once you get the hang of it, you can create some very nice network servers without a lot of code.

Re: Speaking between Perl and C
by mugwumpjism (Hermit) on Jun 03, 2005 at 07:02 UTC

    My advice is to stay away from SOAP. Yes the perlembed interface can be a bit of a PITA.

    Check out how simple it is to write TCP clients and servers using IO::All. I've just implemented an "RPC" mechanism of sorts with it and think it's great. The one-line HTTP server that supports CGI is hilarious.

    If it seems appropriate for your task, I'd have to recommend the Syck library from WhyTheLuckyStiff, which will allow you to convert your structures to a format called YAML. The primary advantage of doing this over XML, for instance, is that the messages are easier to debug, most of the parts are already there, and you're sending data structures using a format designed for data structures and NOT a standard for documentation markup.

    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
      Two comments:
      1. Somebody other than me probably read that comment and immediately wanted to see the Tiny Web Server, so there's a link. And here's the actual code (notice 11-sh unnecessary spaces, so it's actually designed to be readable):
        perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 +? "./$1 |" : $1) if /^GET \/(.*) / })'
      2. The Perl Advent Calendar has another discussion of YAML (using a different library --- perhaps slower than Syck, however?).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://462920]
Approved by kutsu
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-04-19 20:04 GMT
Find Nodes?
    Voting Booth?

    No recent polls found