Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Extending and Embedding in perl Question

by perlsyntax (Pilgrim)
on Dec 02, 2007 at 01:26 UTC ( [id://654373]=perlquestion: print w/replies, xml ) Need Help??

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

When you in embedding a C program into perl you still use some of the C headers file in your embedding into perl program if i am right? where should i start?
  • Comment on Extending and Embedding in perl Question

Replies are listed 'Best First'.
Re: Extending and Embedding in perl Question
by Joost (Canon) on Dec 02, 2007 at 02:03 UTC
    When you mostly want to call C code from perl it's easiest to build a perl module with XS code and use that module like you would any other perl module. XS code is basically C code with a whole lot of macros and extra syntax to make it easy to talk to the perl interpreter and to make it easy to "export" C functions to perl (so they can be called like any other perl function).

    See perlxstut for starters. Because XS code is mostly C, you would have to include any header files you'd normally need to include in a C program plus the header files that XS/perl needs. You would also have to make sure your code is linked to any libraries/objects you need just like regular C (though perl's ExtUtils::MakeMaker can make it reasonably easy to do so portably).

    If your setup really works the other way around (i.e. you're mostly calling into perl from C) you can embed a perl interpreter in your C code and just call perl functions whenever you need them. Apart from how you compile/link your program there isn't really much difference between the two options as far as communicating between the perl and C side of the program is concerned.

    See also, perlapi, perlembed, perlcall, perlxs and the Manning book "Extending and Embedding perl".

Re: Extending and Embedding in perl Question
by perlfan (Vicar) on Dec 02, 2007 at 16:32 UTC
Re: Extending and Embedding in perl Question
by TGI (Parson) on Dec 05, 2007 at 20:05 UTC

    Inline::C is another way to work with C in your perl programs, it allows you to define a chunk of C code and call it like any perl subroutine.

    I've never used the XS stuff, but Inline::C is very straightforward to work with.


    TGI says moo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found