One thing to understand is that Perl references and C pointers are two different things (despite the conceptual similarities).  So,

1. Can I pass a reference to a string and have the C-library fill it for me so that I can de-reference it on return and get the string?

The more natural way would to be to create a new scalar value (SV) and place the C string into it, for example using newSVpv(). Then return that SV to the Perl side, where you can access it as a normal Perl string. See perlapi, perlguts and PerlGuts Illustrated.

2. Can I construct and pass a pointer to a C-struct from my perl module and have it return the struct with values filled in?

Perl doesn't have structs in the sense C has. So you have to convert the contents into an appropriate form, such as a hash with keys corresponding to the struct's fields.

3. Can I pass a pointer to a function and have the C library call it

No. See perlcall for how to do it (it comes with several examples).

See also perlxs, perlxstut and Inline::C for the big picture of how to extend Perl with C.


In reply to Re: Extending with C functions - some basic questions by Anonyrnous Monk
in thread Extending with C functions - some basic questions by unlinker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.