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");

Add to XS.xs

void wretched_hello()

Export the equivalent Perl subroutine in lib/Wretched/XS.pm:

our @EXPORT = qw(

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.

One Response to Wretched::XS

  1. John Bell says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: