Accessing C struct members via T_PTROBJ

Discussion in 'Perl Misc' started by Mark Shelor, Oct 24, 2003.

  1. Mark Shelor

    Mark Shelor Guest

    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
    Mark Shelor, Oct 24, 2003
    #1
    1. Advertising

  2. Mark Shelor

    Sisyphus Guest

    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.
    Sisyphus, Oct 25, 2003
    #2
    1. Advertising

  3. 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Oct 25, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Accessing members of a struct

    , Apr 11, 2006, in forum: C Programming
    Replies:
    5
    Views:
    279
    Barry Schwarz
    Apr 13, 2006
  2. Walter Deodiaus

    accessing inner struct members

    Walter Deodiaus, Dec 29, 2005, in forum: C++
    Replies:
    2
    Views:
    328
    Old Wolf
    Dec 29, 2005
  3. Replies:
    3
    Views:
    280
  4. John Reye
    Replies:
    28
    Views:
    1,345
    Tim Rentsch
    May 8, 2012
  5. Mark
    Replies:
    6
    Views:
    382
    88888 Dihedral
    Jul 9, 2012
Loading...

Share This Page