XSUBは、実際には外部ライブラリが必要ではありません。これらは、perlスペースからac関数を呼び出す機能を提供するだけであり、CとPerlの間の呼び出し規約をマッピングする際の便利さを提供します。
あなたがする必要があるのは、あなたが埋め込んでいるperlインタプリタにあなたが埋め込みアプリケーションにコンパイルしたXSUBを登録することです。
#include "XSUB.h"
XS(XS_some_func);
XS(XS_some_func)
{
dXSARGS;
char *str_from_perl, *str_from_c;
/* get SV*s from the stack usign ST(x) and friends, do stuff to them */
str_from_perl = SvPV_nolen(ST(0));
/* do your c thing calling back to your application, or whatever */
str_from_c = some_c_func(str_from_perl);
/* pack up the c retval into an sv again and return it on the stack */
mXPUSHp(c_str);
XSRETURN(1);
}
/* register the above XSUB with the perl interpreter after creating it */
newXS("Some::Perl::function", XS_some_func, __FILE__);
perlを埋め込む場合、この種のことは通常、渡したxs_init関数で行われますparse_perl
。
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
static void
xs_init (pTHX)
{
newXS("Some::Perl::function", XS_some_func, __FILE__);
/* possibly also boot DynaLoader and friends. perlembed has more
* details on this, and ExtUtils::Embed helps as well. */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
perl_parse(my_perl, xs_init, argc, my_argv, NULL);
その後、Some::Perl::function
perlスペースからXSUBを呼び出すことができるようになり、そのXSUBは、任意の方法でアプリケーションに自由にコールバックできます。