Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Accessing C struct members via T_PTROBJ

Reply
Thread Tools

Accessing C struct members via T_PTROBJ

 
 
Mark Shelor
Guest
Posts: n/a
 
      10-24-2003
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

 
Reply With Quote
 
 
 
 
Sisyphus
Guest
Posts: n/a
 
      10-25-2003
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.

 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      10-25-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Generically accessing members of a struct. david.chanters@googlemail.com C Programming 3 05-24-2009 06:59 PM
Accessing members of a struct joseph.paterson@gmail.com C Programming 5 04-13-2006 02:49 AM
accessing inner struct members Walter Deodiaus C++ 2 12-29-2005 10:19 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57