Ingenious XS

ethan on 2002-12-09T11:08:53

Now that the portability issues of the mplib have been passed to its author who promised to look into that, I can take a little break from that.

Instead I have indeed started with the AA-lib port to XS. It's a most satisfying work now that I have finally grokked how typemaps work. With the help of these a C-lib basically transforms very nicely into an object-oriented Perl lib. A method that sets a pixel in the output is not more than a finger-snip:

void pixel (self, x, y, color) aa_context *self; int x; int y; int color; CODE: aa_putpixel(self, x, y, color);



Called like $aa->pixel(1, 1, $color) and it's done.

And here's the occasional rant: Why don't the XS-docs describe a very easy thing in an easy manner? Till yesterday I thought the typemap would have to translate a C-structure into the equivalent blessed Perl-hash and vice versa. Naturally, with this misconception in mind it's hard to understand the rest of perlxs.pod.

I finally found it out with the help of Dean Roehrich's CookBookA that he has released on CPAN. Mind you, this one doesn't exactly have nice explanations either. It was one sentence that dawned a light on me:

    Ex1 demonstrates how to write a Perl constructor in C.
    The constructor allocates a C structure and creates an
    opaque Perl object to hold an address to the structure.


'hold an address to the structure': That's the sub-sentence that explained Life, the Universe and Everything.


perlxstut ?

rafael on 2002-12-09T12:18:44

Have you read the perlxstut manpage, new in 5.8.0 ? Is it clearer than the older docs ?

Re:perlxstut ?

ethan on 2002-12-09T13:00:26

Err, no. In fact I haven't read that! I made myself acquainted with XS using the perlxstut from 5.6.1. I didn't know there was a new one.

It seems to have one more example. Incidentally it's one dealing with filehandles which is what I will need very soon. That's certainly an improvement!

But the Typemap section is still too thin. It still does not mention the special symbols $var, $arg and $type. I'd love to be able to say perldoc perltypemap and get a concise description of what a typemap file is really for and perhaps one example how it is used to turn a C-struct into a Perl object.

But I don't want to give the wrong impression here: perldocs dealing with XS aren't that bad at all (and perlxstut was enough to get me going with MP3::Mplib in a sort of baby-XS). But they require more experience and patience from their readers than the other perldocs solely dealing with the language Perl.

Re:perlxstut ?

rafael on 2002-12-09T14:24:43

I must've been confused at some point. Perlxstut has only some minor improvements in 5.8.0. That tired brain... (BTW. you know that patches are welcome. A perltypemap man page would be interesting. ;-)

Re:perlxstut ?

ethan on 2002-12-09T21:53:46

(BTW. you know that patches are welcome. A perltypemap man page would be interesting. ;-)

It was only yesterday that I had my first succesful use of a typemap (which was actually copied from Dean Roehrich). A little too thin yet for supplying documentation. ;-)

But I am not at all lazy when it comes to Perl and I'll be the first one to write such a beast if I feel ready. I expect to gain some familiarity with all that soon. Also, when looking at the typemaps available I see a slightly different subset of macros and functions from the perlapi to the ones I have used so far (just this morning I realized that I had no idea how to turn a glob-ref into a PerlIO thing; now I know [btw: sv_2io is unmentioned in perlapi]).

Therefore I am quite confident that the typemap experience will close some gaps I am still suffering from.

Re:perlxstut ?

nicholas on 2002-12-13T11:32:57

BTW. you know that patches are welcome.

Richard Clamp is working on an XS tutorial. The current draft is available here. I believe patches to it are welcome.