c++perlxs

Perl newXS() with closure added


I want to embed Perl in a c++ application and am looking for a method to call into c++ from perl via newXS(). Apart from the function pointer I need the associate a custom pointer to the CV created by newXS(). The pointer contains a C++ context. I dont want to use globals for this. Is there a common way to do this?

On a wider scope the question is maybe weather there is the possibility to add a closure to the CV created by newXS() and how to reference it when the c function is called that was registered with it. CvPADLIST() would seem the perfect place, however for XSubs it seems to be invalid to use when PERL_IMPLICIT_CONTEXT is set (comment in the beginning of perl's pad.c. Can it maybe be ignored?). Is there some other place I can put CV local data?


Solution

  • There's an ANY slot in CV that can be used for custom data and accessed with CvXSUBANY(cv). For example:

    CvXSUBANY(cv).any_ptr = my_ptr;
    

    This slot is normally used to store the index for XS ALIASes and the function pointer for XS INTERFACEs.