in reply to XS Modules - why and when?

Sometimes I use XS to wrap existing C libraries. WiringPi::API is probably one of my larger examples. Not only do I wrap all of WiringPi with it, but within the XS, I often override or wrap the wrapped C functions because I need something to work slightly differently. here is one example.

Other times, I interface with ICs and other devices where clocking bits in and out with precise timing is extremely critical to proper operation (example). That timing I found is often more reliable when written in a compiled language and then presented with a Perl interface.

Other times it's purely for speed for sure.

Sometimes, I've already written something in C for a project that I want to break out into a standalone project, so I put the C code into XS, then present it as a Perl interface. In these cases, I often will also write a Pure Perl port of it. Bit::Manip is one example. That distribution is purely written in C. The XS portion simply presents the C functions to the Perl library.

Sometimes I use XS for academic purposes. I don't write a whole lot of C code so I like to dabble in it from time to time before it fades from my memory completely. Personally I don't write the XS, I write C, then have Inline::C develop the XS for me.

In the case of RPi::DHT11, one has to turn high/low physical GPIO pins to read the data from the sensor. Using the wiringPi library allowed me to do that with an existing interface so I didn't have to write my own. That's why I spent two years wrapping the wiringPi C library in near its entirety. Writing something like that in pure Perl would have been impossible to deal with such low level hardware registers. The timing wasn't the issue here; the manipulation of the GPIO hardware registers was.

-stevieb