I'm unsure about how to call a routine located in another XS module from within an XS moduleOne solution - if, in Foo.xs, we want to access a function (let's call it 'bar') from Bar.xs, then we have the Bar module make available a pointer to the bar function. The Foo.xs functions then access the bar function via that pointer.
I have a full but minimal working demo of this principle on this machine. I could put copies of the files (Foo.pm, Bar.pm, Foo.xs, Bar.xs, Makefile.PL's and test.pl's) on my scratchpad if that's going to serve any purpose. Note that, with this approach, you'll probably be making significant alterations to both source distros. I think it's the best approach to calling xsubs in one extension from xsubs in another extension .... but I'm not so sure that this is the best way for you to achieve your ends in this particular case.
For a basic demo of passing functions by pointer, here's an Inline::C script that
creamygoodness posted a while back:
use strict;
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'END_C';
typedef void
(*hello_func_t)(SV * x);
typedef SV *
(*sv_func_t)(SV * x);
void
hola_mundo(SV * x)
{
printf("Hola, mundo! %d\n", (int)SvIV(x));
}
SV *
hola_mundo2(SV *x) {
printf("Hola, mundo2! %d\n", (int)SvIV(x));
return newSViv((int)SvIV(x) * 2);
}
SV*
get_hola_mundo_func_ptr()
{
return newSViv( PTR2IV(hola_mundo) );
}
SV*
get_hola_mundo2_func_ptr()
{
return newSViv( PTR2IV(hola_mundo2) );
}
void
do_hello(SV *sv_with_func_ptr, SV * x)
{
IV temp = SvIV(sv_with_func_ptr);
hello_func_t hello = INT2PTR(hello_func_t, temp);
hello(x);
}
SV *
do_hello2(SV *sv_with_func_ptr, SV * x)
{
IV temp = SvIV(sv_with_func_ptr);
sv_func_t hello = INT2PTR(sv_func_t, temp);
return hello(x);
}
END_C
my $num = 123458;
my $func_ptr = get_hola_mundo_func_ptr();
do_hello($func_ptr, $num);
my $func_ptr2 = get_hola_mundo2_func_ptr();
my $ret = do_hello2($func_ptr2, $num);
print $ret;
Cheers,
Rob
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.