in reply to Embedding Perl / C++ structure question

The "most obvious" way to pass complex C++ data to perl is to use objects with accessors with a complementary perl XS mapping (so you're using a pointer to a C++ object like a perl object). You want the accessors because although perl can in theory access the data in a foreign object directly, it gets messy very quickly. IIRC in C++ structs are more or less the same as classes so you should be able to transform your structs into classes reasonably easily.

In C++

class SomeObject { public: int get_valueA(); void set_valueA(int value); // etc... }
XS code (you need these to use the methods from SomeObject in perl:
int SomeObject::get_valueA() void SomeObject::set_valueA(int value)
You'll also want to set up a typemap for each class. See also the standard typemap file in your perl distribution for T_PTROBJ_SPECIAL.

See also perlxstut and perlxs and the book "Extending and Embedding Perl".

Replies are listed 'Best First'.
Re^2: Embedding Perl / C++ structure question
by TheGeniuS (Novice) on Nov 28, 2007 at 20:26 UTC
    After a little testing we were able to pass a structure root and have the structure inside perl as we wanted.
    $foo->{whatever}->{whatever} = $bar;
    Obviously now we could send it back using the  $_[n]

    Going to verify your advice and links and eat some pizza :D

    Thank you very much for your prompt reply
      Just a general tip: there's a trade-off between passing a "pure perl" structure and using objects that are really C structs underneath (besides the general pros and cons about encapsulation of classes vs open structs etc):

      If you're going to pass a couple of thousands of structs containing many properties in a list (or nested) and you only really want to query a few of those properties in your perl code, you may well spend much more time converting all the data than you really need, while if you use objects you generally convert the property from perl to C or vice versa each time you access it from perl (but you don't need to convert anything you're not actually accessing).

      Using objects instead of complex nested hashes may also be more efficient when you're modifying the structures, since it's usually trivial to keep the C and perl side in sync when you're using objects, while otherwise you're have to inspect the complete structure each time you pass it from C to perl to C.

        I want to take the time to thank you for all of your help. We have figured everything out and all works fine. Note : we want to inspect the complete structure everytime but I will look into the objects instead of hashes. Thank you so much for everything, you have been very helpful.