This is one of a series of articles about Perl XS, a language used for writing extensions to the Perl interpreter in C.
Math::Ackermann
Set::Bit
XS stands for eXternal Subroutine. With XS, we call C subroutines from Perl code.
XS is the language that specifies how to call C from Perl. XS is also a system of facilities to make this happen: h2xs, ExtUtils::MakeMaker, xsubpp, DynaLoader (all links to search.cpan.org), and the XS language itself.
Some things, like numerical integration, memory-intensive things, device drivers, and things that have already been written in other languages, are better not done in Perl.
Perl provides things like memory management, hash tables, and regular expressions which make it easy to create applications, but have run-time costs.
C gives control over every CPU cycle and byte, so that loops can be fast and data structures can be small. The problem is that every CPU cycle and every byte in the entire program, even parts that aren't processor bound, has to be coded in C, without the memory management and other features of Perl.
With XS, Perl can be used for most of the code, and C for just those parts that require fine control over system resources.
Learning XS from perlxs and perlguts is difficult, because the documentation omits background information.
XS is a collection of macros processed by a program called
xsubpp, where pp is short for Perl
Pseudocode. Xsubpp expands XS macros into bits of C code
to connect the Perl interpreter to C.
To learn XS, you have to learn the Perl C API, Perl's internal data structures, the Perl stack, and how a C subroutine gets access to it. Once you understand all this, you don't need XS: you can code directly to the Perl C API.
However, the Perl C API is repetitive. You have to keep writing the
same little bits of code to move parameters on and off the Perl stack;
to convert data from Perl's internal representation to C variables; to
check for null pointers and other Bad Things. When you make a mistake,
you don't get bad output: you crash the interpreter. The advantage of
wrapping these little bits of code in macros is that you can write
them once and then stop worrying about them. And what do you know,
someone has already written some macros for you; there is even this
macro expander called xsubpp.
Math::Ackermann
Set::Bit
These pages are an adaptation of articles written in 2000 by Steven W. McDougall. My goal in modifying these articles is to simplify them. I hope you find these adapted versions of the articles useful. You can find the original articles at the link at the bottom of this page.
This adaptation is a work in progress and many of the links on these pages may not work.

XS Mechanics by Steven W. McDougall is licensed under a Creative Commons Attribution 3.0 Unported License.