Basically I'm trying to create a file that creates an embedded perl interpreter and exposes various XS functions to the created interpreter. I wish to do this in the same file mainly for simplicity reasons, I actually need to run this code inside of a much larger project and I think it will simplify things a great deal to have easy access to the functions I'm wrapping. Here's the code:
#include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ #include <XSUB.h> #include <stdlib.h> static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ void say( char *m ) { printf("I am say: %s", m ); } XS(XS_Module_say); /* prototype to pass -Wmissing-prototypes */ XS(XS_Module_say) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: Module::say(message)"); { char * message = (char *)SvPV_nolen(ST(0)); say(message); } XSRETURN_EMPTY; } #ifdef __cplusplus extern "C" #endif XS(boot_Module); /* prototype to pass -Wmissing-prototypes */ XS(boot_Module) { dXSARGS; char* file = __FILE__; XS_VERSION_BOOTCHECK ; newXS("Module::say", XS_Module_say, file); XSRETURN_YES; } int main(int argc, char **argv, char **env) { char *embedding = { "", "-e", "0" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
The problem is when I run it, I see no sign of my XS function ("say") in either main:: or Module::. It occurred to me that perhaps I need to call "boot_Module", which apparently takes two arguments: a perl_interpreter, which I have, and a CV*, which I don't. I've tried various combinations of arguments to boot_Module, but it constantly segfaults. What is the right thing to pass?

In reply to Embedding perl and exposing xs functions by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.