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


In reply to Re: XS Modules - why and when? by stevieb
in thread XS Modules - why and when? by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.