Complex extensions in C

Discussion in 'Perl Misc' started by arne.muller@gmail.com, Sep 16, 2006.

  1. Guest

    Hello,

    I'm looking for some resources (books, web-sites, docs ...) on how to
    write complex perl extensions in C. I've read the perlxstut, but still
    don't understand how to convert complex types.

    Specificly, my C-routine needs a pointer to a structure, some members
    of this structure are pointers to type double arrays. On the perl side
    this structure is an object. The return value of the C function is a
    different type of structure, but again containing pointers to array (on
    the perl side this is an object).

    Maybe you can point out some places on the web or news grou articles (I
    didn't find much!).

    greetings,

    Arne
    , Sep 16, 2006
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hello,
    >
    > I'm looking for some resources (books, web-sites, docs ...) on how to
    > write complex perl extensions in C. I've read the perlxstut, but still
    > don't understand how to convert complex types.
    >
    > Specificly, my C-routine needs a pointer to a structure, some members
    > of this structure are pointers to type double arrays. On the perl side
    > this structure is an object. The return value of the C function is a
    > different type of structure, but again containing pointers to array (on
    > the perl side this is an object).


    So you want to translate Perl objects (which presumably contain lists of
    numbers) into C structs for consumption by XS routines. The basic
    method is to use pack() to build the struct in a string. You pass the
    string to the XS routine as such and cast it to a (pointer to) the type
    of struct you have built. If you have got it right (which will be
    non-trivial), you'll find a workable struct in your XS program.

    You'll have to use the cumbersome "p" template to create pointers
    from one part of the string to another.

    Anno

    > Maybe you can point out some places on the web or news grou articles (I
    > didn't find much!).
    >
    > greetings,
    >
    > Arne
    >
    -berlin.de, Sep 16, 2006
    #2
    1. Advertising

  3. Ben Morrow Guest

    Quoth -berlin.de:
    > <> wrote in comp.lang.perl.misc:
    > > Hello,
    > >
    > > I'm looking for some resources (books, web-sites, docs ...) on how to
    > > write complex perl extensions in C. I've read the perlxstut, but still
    > > don't understand how to convert complex types.
    > >
    > > Specificly, my C-routine needs a pointer to a structure, some members
    > > of this structure are pointers to type double arrays. On the perl side
    > > this structure is an object. The return value of the C function is a
    > > different type of structure, but again containing pointers to array (on
    > > the perl side this is an object).

    >
    > So you want to translate Perl objects (which presumably contain lists of
    > numbers) into C structs for consumption by XS routines. The basic
    > method is to use pack() to build the struct in a string. You pass the
    > string to the XS routine as such and cast it to a (pointer to) the type
    > of struct you have built. If you have got it right (which will be
    > non-trivial), you'll find a workable struct in your XS program.
    >
    > You'll have to use the cumbersome "p" template to create pointers
    > from one part of the string to another.


    Note that there are nontrivial issues regarding struct packing and the
    like (you will likely need a little C program to construct the pack
    template for you using offsetof). You can also pass a hash to an XS
    routine that then builds the struct by pulling the members out of the
    hash in C.

    It may be easier to make the Perl side properly OO, or implement a tied
    hash/array, and keep the data in 'C format' all the time.

    Ben

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    Heraclitus
    Ben Morrow, Sep 16, 2006
    #3
  4. Sisyphus Guest

    <> wrote in message
    ..
    ..
    >
    > Specificly, my C-routine needs a pointer to a structure, some members
    > of this structure are pointers to type double arrays. On the perl side
    > this structure is an object.


    For some basics on objects see
    http://search.cpan.org/~ingy/Inline-0.44/C/C-Cookbook.pod#Object_Oriented_Inline .
    There might be some other basic information on the same page that's useful,
    too.

    Cheers,
    Rob
    Sisyphus, Sep 17, 2006
    #4
  5. Guest

    wrote:
    > wrote:
    > > Hello,
    > >
    > > I'm looking for some resources (books, web-sites, docs ...) on how to
    > > write complex perl extensions in C. I've read the perlxstut, but still
    > > don't understand how to convert complex types.
    > >
    > > Specificly, my C-routine needs a pointer to a structure, some members
    > > of this structure are pointers to type double arrays. On the perl side
    > > this structure is an object.

    >
    > A Perl object or merely an opaque object held in Perl? I.E. does Perl have
    > to do anything with it, other than just pass the root pointer back into C?


    Well, this should be a real perl object. The part that I'd like to
    implement in C is just part of a larger application in which this
    object lives ...

    Arne

    > Xho
    >
    > --
    > -------------------- http://NewsReader.Com/ --------------------
    > Usenet Newsgroup Service $9.95/Month 30GB
    , Sep 17, 2006
    #5
  6. Guest

    Ben Morrow wrote:
    > Quoth -berlin.de:
    > > <> wrote in comp.lang.perl.misc:
    > > > Hello,
    > > >
    > > > I'm looking for some resources (books, web-sites, docs ...) on how to
    > > > write complex perl extensions in C. I've read the perlxstut, but still
    > > > don't understand how to convert complex types.
    > > >
    > > > Specificly, my C-routine needs a pointer to a structure, some members
    > > > of this structure are pointers to type double arrays. On the perl side
    > > > this structure is an object. The return value of the C function is a
    > > > different type of structure, but again containing pointers to array (on
    > > > the perl side this is an object).

    > >
    > > So you want to translate Perl objects (which presumably contain lists of
    > > numbers) into C structs for consumption by XS routines. The basic
    > > method is to use pack() to build the struct in a string. You pass the
    > > string to the XS routine as such and cast it to a (pointer to) the type
    > > of struct you have built. If you have got it right (which will be
    > > non-trivial), you'll find a workable struct in your XS program.
    > >
    > > You'll have to use the cumbersome "p" template to create pointers
    > > from one part of the string to another.

    >
    > Note that there are nontrivial issues regarding struct packing and the
    > like (you will likely need a little C program to construct the pack
    > template for you using offsetof). You can also pass a hash to an XS
    > routine that then builds the struct by pulling the members out of the
    > hash in C.
    >
    > It may be easier to make the Perl side properly OO, or implement a tied
    > hash/array, and keep the data in 'C format' all the time.


    Maybe it's easier to write a wrapper routine in perl that takes the
    perl object, extracts it's attributes (scalars and arrays) and passes
    it to a C-wrapper routine which re-creates the proper C-structs from
    it's function arguments. Finally the C-routine that takes athe pointe
    rargument is called from this wrapper. This means the perl object gets
    copied, and there's a perl object and a C struct. This may just be fine
    for me.

    regards,

    Arne

    > Ben
    >
    > --
    > The cosmos, at best, is like a rubbish heap scattered at random.
    > Heraclitus
    >
    , Sep 17, 2006
    #6
  7. Peter Scott Guest

    Peter Scott, Sep 18, 2006
    #7
    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. thewhizkid

    Help! list all file extensions

    thewhizkid, Nov 19, 2003, in forum: Perl
    Replies:
    1
    Views:
    438
    J├╝rgen Exner
    Nov 19, 2003
  2. news.amnet.net.au
    Replies:
    1
    Views:
    573
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  3. Stanimir Stamenkov
    Replies:
    2
    Views:
    744
    Stanimir Stamenkov
    Oct 25, 2005
  4. Robert Mark Bram
    Replies:
    0
    Views:
    682
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    757
Loading...

Share This Page