Hi,
The
perlcall documentation has a number of fine examples of an XSub retrieving a perl sub's return value. But my requirement is that the XSub then return the value that the perl sub returned (and I didn't see an example of that in the perlcall documentation).
Based on that documentation,I came up with this:
use warnings;
use Inline C => Config =>
BUILD_NOISY => 1;
use Inline C => <<'EOC';
SV * call_Adder(SV * a, SV * b) {
dSP;
I32 ax;
int count;
PUSHMARK(SP);
XPUSHs(a);
XPUSHs(b);
PUTBACK;
count = call_pv("Adder", G_SCALAR);
SPAGAIN;
SP -= count;
ax = (SP - PL_stack_base) + 1;
if (count != 1)
croak("Big trouble\n");
/* Avoid "Attempt to free unreferenced scalar" warning */
SvREFCNT_inc(ST(0));
return ST(0);
}
EOC
my $val = call_Adder(13,118);
print $val, "\n";
my $val2 = call_Adder(14,119);
print $val2, "\n";
my $val3 = call_Adder(14,8);
print $val3, "\n";
sub Adder {
my($a, $b) = @_;
$a + $b;
}
__END__
Outputs (as expected):
131
133
22
As can be seen, the "call_Adder" XSub passes its arguments on to the "Adder" perl sub, and then returns the same value as "Adder" itself has returned.
Have I committed any sins with that approach ?
It varies from the perlcall examples mainly in that there's no ENTER/LEAVE and SAVETMPS/FREETMPS calls.
It seems to be working fine for me, but corrections and/or suggestions for improvement are welcome. (It's planned that this approach will eventually make it into some of my modules, so it's probably better to sort out the shortcomings *before* that happens.)
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.