All,
I rarely try and mix C and perl. When I do, I use
Inline::C very sparingly and then use tools to convert to XS for me. I am not a very strong C programmer to begin with so I haven't ever bothered to try and digest the perl API other than whatever that one little bit is to get whatever my current dilema working. Ok, now with that disclaimer of ignorance out of the way, I would like to know how to keep a variable created in C land "alive" when passed back to perl and then back to another C function. Here it is in very broken pseudo code
my $tree = new_tree();
print node_count($tree), "\n";
__END__
__C__
typedef struct {
int count;
} Tree;
SV* new_tree () {
Tree* tree;
tree.count = 0;
/* return tree some how */
}
int node_count (SV* tree) {
/* get back at tree some how */
return tree.count;
}
I have seen the Inline::C-Cookbook example of OO. I don't want an object. If this were pure C, I would pass in a pointer to new_tree and not return anything (glossing over malloc and free). In the CB, tye mentioned "allocate PV space in an SV and stuff it there" which conceptually I understand but have no idea on the implementation. Feel free to mention perlapi, perlxstut, perlguts illustrated, etc though I have all of them on my reading list. I would like a working example to play with as I read though. I know this means I am going to have to handle memory management myself and I am fine with that.
Thanks in advance.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.