in reply to Re: Perl XS
in thread Perl XS
Thanks!. I have seen Inline::C. I wanted to give XS a try. I still wanted to use XS as it keeps an abstraction.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Perl XS
by syphilis (Archbishop) on Nov 19, 2013 at 00:11 UTC | |
For example, I placed (copy'n'paste') your 2 functions in an Inline::C script: When I run that script, it compiles, then outputs: The problem is that, although there's nothing syntactically wrong with print_array_int(), perl doesn't know how to pass the 'int array[]' type to XS. For a working solution, you need to know a little bit about the perl API. I suggest perlxs, perlxstut, and perlapi docs, though you'll perhaps also find some useful tips in the Inline::C cookbook and, no doubt, many other places. Anyway, here's one solution: "items" is the number of elements on the stack - so there's really no need to pass the length of the array to the function. You could remove that arg and rewrite the for loop condition as (i=0; i<items; i++) Two things to note about Inline::C: 1) It's really just XS - it takes your C code, autogenerates the XS code, then compiles and runs your program. 2) Inline::C defines its own stack macros, all of which begin with "Inline_Stack_" and are defined in the Inline.h file that it also autogenerates. Other than that, it's the same as XS, and you don't *have* to use its stack macros. You can just use the normal XS terms - as I did above when I declared "dXSARGS" instead of "Inline_Stack_Vars" (and used "XSRETURN(0)" instead of "Inline_Stack_Void"). Cheers, Rob | [reply] [Watch: Dir/Any] [d/l] [select] |
by davido (Cardinal) on Nov 19, 2013 at 00:26 UTC | |
I second this! Here's one way to do it... It's really so much more convenient. One thing to keep in mind: Passing a char* string around is simple until you start dealing with Unicode. Eventually it becomes easier to pass an SV*, and avoid touching the internal PV string except with proper XS macros/functions, and even then with extreme care. Also, write your unit tests either as step zero, or in conjunction with step one. That way when you've re-implemented in Inline::C/XS you can verify behavior. Dave | [reply] [Watch: Dir/Any] [d/l] [select] |
In Section
Seekers of Perl Wisdom