Dear Sir,

I am writing to thank you very much for your quick and very helpful
reply (I can hardly wait to try it out).

On Mon, Jan 13, 2003 at 12:41:54PM +0000, Nick Ing-Simmons wrote:
> >1 I can only return results to Perl by an explicit XSRETURN(items) after 
> >the XPUSHs. 
> 
> Yes.
> 
> >
> >Therefore I have to accumulate the number of XPUSHs in a global variable 
> >updated by the C callback (server_fn() above)).
> >
> >Is that correct ?
> 
> But it already is - the perl stack pointer that is incremented by XPUSH().

Unfortunately, I couldn't work out from what have I read, how to get the
SP updated properly (the XS was returning only the value of the parm -
object pointer - that the XS (the function that calls cli_print_queue())
was called with) XPUSHs in the callback wasn't sufficient. 

> Also what is the 'int' that function returns? Is that any help? 
>

Yes, I should have looked more carefully at that. The return value of
the cli_print_queue function is how many times the callback is called ie
the number of elements to return.
 

> >What is 'the recommended way' or what are some of the less obvious 
> >tradeoffs in each of these ways ?

(It works Ok with the callback pushing onto a global AV and the XS
pushing each popped value from the AV onto the argument stack, but the
disadvantages are pretty obvious ...)

> 
> /* In the C part before the MODULE line */
> void 
> server_fn(struct print_job_info *p) 
> {
>  dSP;
>  XPUSHs(sv_2mortal(newSVpvf()))
>  PUTBACK;
> }
> 
> ...
> 
> /* Now the XS part */
> MODULE ....
> void
> cli_print_queue(struct cli_state *cli)
> PPCODE:
>  {
>   SV **start = SP;
>   int ret;
>   PUTBACK;  /* so global variable is up-to-date */
>   ret = cli_print_queue(cli,server_fn);
>   SPAGAIN;  /* Get SP after the XPUSHs */
>   /* Do whatever is necessary for return value in 'ret' */
>   XSRETURN(SP-start); /* Count of number pushed */
>  } 
> 

Thank you very much for this. (Obviously I have a lot more thinking to
do about the stack macros and how it all works).

> -- 
> Nick Ing-Simmons
> http://www.ni-s.u-net.com/
> 

FWIW, I like XS programming (?!) as much as I like coding in Perl -
that is I couldn't imagine being more productive (relatively
speaking) in any other language. 

It seems to me that XS has a lot to offer the novice/hack C programmer
in that it provides access to a superbly rich set of data structures,
that are so much more powerful and easy to use than those of C, while
not forsaking the elegance and speed of C.

Why have I ever bothered trying to define my own data structures in C ?
Why have I ever tried to use C strings when I could be using Perl SVs
? etc etc

Do people actually embed Perl simply so they can take advantage of the
great API ? If not, why not ? (the Couzins and Jenness book suggests
that the application can use more memory, be less stable, have leaks
etc).

Would it be worth attempting to convert an application based on linked
lists (and hence crippled by linear searches for large amounts of
data) to use HV *'s ? (especially since the application
[Netsaint/Nagios] already can be built with an embedded interpreter).

Thank you very much,

Yours sincerely.


--
------------------------------------------------------------------------
Stanley Hopcroft
------------------------------------------------------------------------

'...No man is an island, entire of itself; every man is a piece of the
continent, a part of the main. If a clod be washed away by the sea,
Europe is the less, as well as if a promontory were, as well as if a
manor of thy friend's or of thine own were. Any man's death diminishes
me, because I am involved in mankind; and therefore never send to know
for whom the bell tolls; it tolls for thee...'

from Meditation 17, J Donne.

Reply via email to