C is not Perl

chromatic on 2006-10-02T07:07:03

Stevan asked me to write some XS code for Class::MOP, and it sounded like more fun than painting my deck or writing the non-fiction book, so I dodged segfaults for a couple of hours this afternoon.

The Perl XS API has its own kind of consistency. It's never really stuck in my brain, but it's just familiar enough that I sort of know what I want.

In theory, you can do anything from C that you can from Perl. In practice, try manipulating typeglobs from C. Though deleting an element from a typeglob is straightforward in Perl (once you realize that you can't really delete, you just have to create a new glob, copy in everything you don't want to delete--if it exists--, then assign the new glob in the old symbol table), that's really difficult in C. I don't just mean dealing with refcounts and garbage collection, though that's a little tedious. You can get some seriously complex code that goes seriously wrong in many weird ways.

Then I asked myself what I was doing and why I couldn't just delete the elements. It works that way, even with using the standard XS macros.

(I showed The GV and GP sections of PerlGuts Illustrated to a friend who said it looked like gibberish. Sadly, I know what it's talking about and it looks mostly gibberish to me too.)


Shame XS wasn't "better"

Matts on 2006-10-02T12:58:56

If XS could have been a bit more pre-processed than it is, it could have been so much better. Things like accessing hash values and references in XS is more painful than it could be with a little pre-filtering.