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 又可以自由地以任何它想要的方式回调到您的应用程序。