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

Hi,

I've embedded Perl into a server written in C. Perl's job is to manipulate some data structures and do other general glue work. My question is if there exists a way to make the perl code call back C code?

Ie, some data comes in, C initializes some data structures and sends a reference to them to a Perl sub, which fiddles them around a bit, and then in certain circumstances calls a C function. (Or a Perl wrapper to a C function or whatnot).

The closest I've found would be a C XS extension, but will the master program and the C extension exist in the same namespace? Or is there a better way to do what I want?

Thanks in advance,
Ryan

Replies are listed 'Best First'.
Re: Embedded perl question
by salva (Canon) on Dec 22, 2005 at 10:25 UTC
    write an XS module wrapping the C calls you want, link the .c file generated form the .xs on your program, and call its boot_Foo function just after initilizing the perl interpreter. After that, you should be able to call the wrapped functions from your embedded perl.

    About the namespace, if you want your functions to live in main::, just indicate it with the PACKAGE directive inside the XS file.