Like others above, I would recommend Inline::C as an "easy" way to do XS. There are some tricks you can even do to let vanilla perl handle your memory management -- see for example the snippet at
Pattern for Inline-C-based crunching. That snippet talks about using a packed perl string as the data source to pass data in to a C function, but the exact same technique could be used to get data out (if you know how big the result will be).
For example, if you knew you were passing in an array of 15 doubles and were getting back an array of 15 doubles (with the obvious extension if the sizes were not the same), you could do:
$inval = pack("d*",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
$results = pack("d*",(0)x15)
$summary_result = c_function($inval,$results,15)
@real_results = unpack("d*",$results)
use Inline C => <<'END_OF_C_CODE';
double c_function(char * the_data, char * results,int nelems) {
double *real_data = (double *) the_data;
double *real_results = (double *) results;
double summary_result;
/* do your fast calculation here, storing elements in *real_resul
+ts */
return summary_result;
}
END_OF_C_CODE
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.