Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: A few very basic questions about Extending Perl

by juster (Friar)
on Dec 26, 2010 at 17:58 UTC ( #879222=note: print w/replies, xml ) Need Help??


in reply to A few very basic questions about Extending Perl

When you use av_make perl copies the scalar data into the new (Perl, not C) array so you should free the temporary C array you have created. You should instead use newAV and av_store when you do not already have a C array of SV *'s handy.

Perl uses a stack to pass a list of scalars to your subroutine and returns a list of scalars using this stack. When you assign a sub's return value to a perl array, it is stuffing this list into your new array. However it is actually not possible to return a raw array.

The stack can only returns scalars which is why Inline::C automatically returns a reference to a hash or array for you. This also screws up your reference counts. You could use sv_2mortal to properly decrement the reference count in a delayed manner. Read about it more in perlguts for the long explanation.

To return the list you want, you can push values onto the return stack like cdarke has mentioned. When using XS you use XPUSHx to push to the stack directly. For Inline::C it looks like you would use Inline_Stack_Push and the other Inline_Stack macros instead.

Other than the relevant perl manpages, this is the only webpage on XS that helped me any: http://world.std.com/~swmcd/steven/perl/pm/xs/intro/. I haven't used Inline::C very much but I hope that helps.

Replies are listed 'Best First'.
Re^2: A few very basic questions about Extending Perl
by unlinker (Monk) on Dec 26, 2010 at 19:57 UTC
    Thanks - The Extending/Embedding Perl book and the set of articles by Steven McDougall are both excellent resources. The articles are especially a great resource and would have saved me a lot of time if I had discovered them earlier. Thank you both once again
Re^2: A few very basic questions about Extending Perl
by unlinker (Monk) on Dec 29, 2010 at 08:58 UTC
    Just to make the references here complete, I found the discussion on the XS library here very useful too.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://879222]
help
Chatterbox?
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-06-21 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (118 votes). Check out past polls.

    Notices?