On Thu, 2003-10-30 at 16:14, Steve Hay wrote:
> 
> SV *
> hello()
>     PREINIT:
>         char    *chartext;
>         SV    *svtext;
> 
>     CODE:
>         Newz(0, chartext, 13, char);
>         strcpy(chartext, "Hello, world");
>         sv_usepvn(svtext, chartext, 13);
>         RETVAL = svtext;
> 
>     OUTPUT:
>         RETVAL
> 

I may be misunderstanding this but, from first principles, this is what
I think is going on.

SV *
hello()
CODE:
{
        char *p = "hello world";
        RETVAL = newSVpvn(p, 0);
}
OUTPUT:
        RETVAL

is equivalent to (after removing extraneous brackets and comments) 

{
  char *p = "hello world";
  RETVAL = newSVpvn(p, strlen(p));

  ST(0) = RETVAL;
  sv_2mortal(ST(0));
   
  XSRETURN(1);
}


Obviously this can be cleaned up a bit to:-

SV *
hello()
PPCODE:
{
        char *p = "Hello World";
        ST(0) = sv_2mortal(newSVpvn(p, strlen(p))));
        XSRETURN(1);
}

Unless you are prepared to do a *lot* of extra work I would allow perl
to copy the contents of your existing data area that you are returning.

Otherwise you will need to make sure that you are using the malloc that
you think you are and also maybe even have specific DESTROY methods and
things (depending on precisely what you are trying to return here).

(and before anyone asks, the braces after the CODE: segments are there
to cope with the fact that xsubpp generates invalid code if you have
typemapped references to data that you are trying to check as belonging
to the correct class (a la T_PTROBJ)). It is simply force of habit.

And, BTW, I stopped using Inline::C etc within 2 days of starting to use
it. It is far too limited. It is useful as an entry point to learning XS
though.

The existing documentation may be good as a reference point, but it
really doesn't tell you what you want to know (especially as a
beginner). 

Also XS is actually quite straight forward. There are simple rules; just
stick to them and don't try to be clever! Look at the generated C code,
things then become a lot clearer.

Dirk 

Reply via email to