In addition to tye's great advice, I will also suggest that you check out
Inline::C, which makes writing XS code almost transparent. You can just embed C code right in your perl code, and not need to worry about a lot of the niggling details of XS.
I posted a snippet (
Pattern for Inline-C-based crunching) to show samples of doing this sort of thing for one-dimensional arrays returning single scalar values. Tye's example is a great way to extend this to get more complex data in and out.
++tye.
--JAS