I wrote my first hello-world XS module the other day. Called Wretched::XS, it does nothing more than print, “Hello wretched world.”
As with all hello worlds it was quite simple. Generate the skeleton of the module like so:
h2xs -A -x -n Wretched::XS
cd Wretched-XS
Make a file wretched.h:
#include <stdio.h>
void wretched_hello ();
and a file wretched.c
#include "wretched.h"void wretched_hello() {
printf ("%s", "Hello wretched world");
return;
}
Add to XS.xs
void wretched_hello()
Export the equivalent Perl subroutine in lib/Wretched/XS.pm:
our @EXPORT = qw(
wretched_hello
);
In Makefile.PL, add the object files to be linked:
OBJECT => 'XS.o wretched.o',
Build it…
$ perl Makefile.PL
$ make
$ make test
$ make install
…and give it a try.
% perl -MWretched::XS -e 'wretched_hello';
Hello wretched world
That is about as simple as XS gets.
Stayed tuned for further adventures in XS.
Very nicely done.
As you make your XS modules more and more sophisticated/useful, I think it would be a great help to the community if you detail how Perl data structures get exposed in the XS code. That seems to me (a reasonably experienced, very-long-time Perl programmer with even longer C programming history) to be the most difficult part of the exercise.
Thanks for this thread.