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.

Share this: Twitter

Facebook

Like this: Like Loading... Related