Accessing C struct members via T_PTROBJ

M

Mark Shelor

I recently wrote a Perl extension in which h2xs pretty much handled all
the XS details for me. I now need to go back and access C struct
members from within Perl if possible.

The typemap file indicates that my struct pointer (SHA *) got mapped as
a T_PTROBJ. Is there any way I can use this info to do reads and writes
on the various struct members? Or, do I need to go back and write
accessor routines in C?

TIA, Mark
 
S

Sisyphus

Mark said:
I recently wrote a Perl extension in which h2xs pretty much handled all
the XS details for me. I now need to go back and access C struct
members from within Perl if possible.

The typemap file indicates that my struct pointer (SHA *) got mapped as
a T_PTROBJ. Is there any way I can use this info to do reads and writes
on the various struct members? Or, do I need to go back and write
accessor routines in C?

TIA, Mark

I was involved in some discussion of Inline::Struct about a week ago -
though I don't profess to know much about it. It's not yet recommended
for production, but it might be useful to you. (It uses the Inline and
Inline::C modules to enable direct reads and writes on C struct members
form perl.) If you think it might be useful then first check out the
*entire* thread "Help w/ Inline::Struct" on the Inline mailing list
archives - see http://lists.perl.org, click on the link to Inline, then
on the link to the Archive.

Reading that thread should give you a fair idea of what you need to do
if you want to make use of Inline::Struct.

I think it turned out to be insufficiently versatile for the purposes of
the OP, and he concluded that he would have to write accessor routines
in C - which is an option you've mentioned, and is the only additional
option I'm aware of .... but faik, there could be others :)

Cheers,
Rob
 
T

Tassilo v. Parseval

Also sprach Mark Shelor:
I recently wrote a Perl extension in which h2xs pretty much handled all
the XS details for me. I now need to go back and access C struct
members from within Perl if possible.

The typemap file indicates that my struct pointer (SHA *) got mapped as
a T_PTROBJ. Is there any way I can use this info to do reads and writes
on the various struct members? Or, do I need to go back and write
accessor routines in C?

Usually, yes. This can be annoying for large structs but it's the price
you have to pay when speeding up Perl with a static language. Keep in
mind that there is no portable way to access members of a struct
dynamically in C so you always need to specify them literally. There are
some tricks to get at the byte offset of a member in a structure and
those could be used to read and set the values. But, as I said, those
are not portable. You'll find a demonstration of this technique in the
comp.lang.c FAQ.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top