Steve Hay <[EMAIL PROTECTED]> writes:
>>
>I had looked at using a CODE: section before, but two things in the
>perlxs manpage put me off:
>
>In the section on PPCODE it says:
>
>"Occasionally one will want an XSUB to return a list of values rather
>than a single value. In these cases one must use PPCODE: and then
>explicitly push the list of values on the stack."
>
>Why does it say that you _must_ use PPCODE if CODE is actually OK? Is
>that a documentation error?
It is certainly far too strong. It does say though that XSRETURN macros
work in CODE. So I think what it is trying to say is that for implicit
(fall-off-the-end) style XS you need to use PPCODE.
>
>Also, in the section on RETVAL it says:
>
>"If PPCODE: directive is not used, void return value should be used only
>for subroutines which do not return a value, even if CODE: directive is
>used which sets ST(0) explicitly."
>
>This also instructs me not to use CODE since my XSUB declaration is "void".
I routinely use
void
foo(whatever)
CODE:
{
ST(N) = ...
XSRETURN(M);
}
To return 0,1,...,N results.
I think that provided you use an XSRETURN it is safe.
>
>So it seems there are problems using PPCODE or CODE, which leaves me
>rather confused. Which is the lesser of the two evils?
I personally avoid PPCODE as the incoming args have been popped, so
if your XS calls back into perl they get scribbled over, (so you need
to take copies or extract vital data at start of routine - which does
not always suit semi-automaticly-generqated Tk code ).
However as docs suggest using PPCODE style you would be on stronger
moral ground doing it that way.
>
>Either version of the simple XSUB above works fine, but I just wanted to
>try and get things straight in my mind before I move onto bigger XSUBs
>in the future.
The only real way to get things straight in ones mind is to look
at the generated C code and then readup on the macros it uses.
The docs are still rather weak on how argument/return stack works.
XS code is great for wrapping existing C code with simple returns.
For cases where you want to make more effort to make the interface
more perl-ish then I find that a
void
Foo(...)
CODE:
{
...
XSRETURNxxxx();
}
is easiest way to get a "raw" block where I can do exactly what I want.
But then I know what I am doing (mostly!) - I have even got XSUBs coded
straight in C
So _my_ style is tends to be either
RetType
Func(args,here)
With no other directives at all, or a CODE: block with an XSRETURN