Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Accessing C struct members via T_PTROBJ (http://www.velocityreviews.com/forums/t883272-accessing-c-struct-members-via-t_ptrobj.html)

Mark Shelor 10-24-2003 09:17 PM

Accessing C struct members via T_PTROBJ
 
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


Sisyphus 10-25-2003 09:45 AM

Re: Accessing C struct members via T_PTROBJ
 
Mark Shelor wrote:
> 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

--
To reply by email u have to take out the u in kalinaubears.


Tassilo v. Parseval 10-25-2003 10:05 AM

Re: Accessing C struct members via T_PTROBJ
 
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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval


All times are GMT. The time now is 02:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.