in reply to Embedding Perl in C - C structs
Create perl object from C struct:
/* assume "buffer" is a C struct */ /* assume "package" is a perl string naming the package to bless the object in */ SV* self = newSViv(PTR2IV(buffer)); SV* ref = sv_bless(newRV_noinc(self),gv_stashsv(package,1)); /* ref is now a perl object containing a pointer to the "buffer" s +truct */
and vice versa:
if (sv_derived_from(sv, "Expected::Package")) buffer_pointer = (buffer_type*) SvIV((SV*)SvRV(perlobject));
See the perlxstut manpage for an intro into writing XS.
update: ofcourse, you then need to write accessor methods in XS to handle getting/setting struct data. Also, I recommend reading the "Extending and Embedding Perl" book.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Embedding Perl in C - C structs
by ReinhardE (Sexton) on Feb 05, 2005 at 09:20 UTC |