Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: A problem converting C code to XS

by tachyon (Chancellor)
on Nov 16, 2004 at 02:20 UTC ( #408020=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://408020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-08-16 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (169 votes). Check out past polls.

    Notices?