On Sep 8, 2004, at 7:07 AM, Pawel Matykiewicz wrote:

This is the same as @list = ( [] ); but I need @list = (); in my C function. How to do it ? Assuming that I don't want to edit the *.xs files.

you're not going to get very far if you don't want to edit the *.xs files. to return a list from an xsub you need to use PPCODE and push the various return values onto the stack. the built-in typemaps include some "array" manipulators (the T_ARRAY typemap), but you still have to write some support code for those, too, and i can't find mention of it in the manpages.


i usually do something like this:

## return type of PPCODE must alway be void
void
gimme_a_list (int len)
PREINIT:
int i;
PPCODE:
/* make space for len items on the stack */
EXTEND (SP, len);
for (i = 0 ; i < len ; i++)
/* sv_2mortal() makes sure the value will be reaped if it's
* not captured by the calling code. very important. */
PUSHs (sv_2mortal (newSViv (i)));


you can then call this as

  @list = gimme_a_list (5);
  print join(", ", @list)."\n";
  # prints
  # 0, 1, 2, 3, 4


--
That's it! It's one thing for a ghost to scare my children, but it's another to play my theremin!
- Homer Simpson




Reply via email to