in reply to A problem converting C code to XS
The heart of XS is really dealing with type conversion. XS can deal with some type conversions itself via its inbuilt typemap. You really have two issues. One is how you plan to pass around your C structs. You will almost certainly need to have a custom typemap (see perlxs tutorial) The second appears to be how to return strings or multiple values from your C function. You can quite happily do this in several ways:
use Inline 'C'; my $str = string1(); print $str; my $retval = string2($str); print $str, "And the answer is $retval\n"; __END__ __C__ /* Reset the stack and push SV* onto it */ void string1() { char * set = "Hello \0"; dXSARGS; sp = mark; XPUSHs(sv_2mortal(newSVpv(set,strlen(set)))); PUTBACK; } /* Modify passed SV* C style, also return an int */ int string2( SV * str ) { char * set = "World!\n\0"; sv_setpvn( str, set, strlen(set) ); return 42; }
cheers
tachyon
|
|---|