Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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 browsing the Monastery: (3)
As of 2018-12-11 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (52 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!