On Tue, Aug 26, 2003 at 05:03:01PM -0400, Tao Wang wrote:
> I'm trying to write a C function that will return a scalar value. In
> between, the C function will call back to a Perl function to help it
> along. I have read that I shouldn't use Inline_Stack_Vars when calling
> back to Perl so I'm using the examples in perldoc percall instead. Here's
> a skeleton example of what I'm trying to do:
*snip*
> The exclaim subroutine just appends a "!" to a scalar. I figure when
> call_pv("exclaim", 0) is called, the return value of exclaim is now on the
> stack. How do I get this scalar to be returned to Perl (since I can't use
> Inline_Stack_Return(n) since Inline_Stack_Vars isn't being used)?
>
> --Tao
You can return an SV* directly from your C functions, I've done that
often with good results. Going with your example:
use strict;
use warnings;
use Inline C => 'DATA';
sub exclaim {
my $string = shift;
return "$string!";
}
my $s = call_exclaim("Hello");
print "s=$s\n";
exit 0;
__DATA__
__C__
SV*
call_exclaim( SV* string ) {
SV *result;
dSP;
int count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVsv(string)));
PUTBACK;
count = call_pv("exclaim", 0);
SPAGAIN;
if (count != 1) {
croak("Big trouble\n");
}
result = POPs;
SvREFCNT_inc(result);
PUTBACK ;
FREETMPS ;
LEAVE ;
return result;
}
Hope this helps,
Kyle R. Burton
--
------------------------------------------------------------------------------
Wisdom and Compassion are inseparable.
-- Christmas Humphreys
[EMAIL PROTECTED] http://www.voicenet.com/~mortis
------------------------------------------------------------------------------