How about eval_pv'ing a string containing the contents of the file, plus "DynTrans->import;"? use DynTrans; is roughly equivalent to BEGIN { require DynTrans; DynTrans->import; }. eval_pv'ing the contents of DynTrans.pm is like require-ing it. Add a "package main;", before the import, and voila!

Tested this, and it works:

Key lines:

/* moduletext = text of module + "package main; DynTrans->import;" */ eval_pv(moduletext, TRUE); /* ~ use DynTrans; */ eval_pv("$a = fortytwo();", TRUE); /* its function is available */

Full C source

#include <EXTERN.h> #include <perl.h> static PerlInterpreter *pi; /* Note the last two lines "package main; DynTrans->import" */ static char *moduletext = "package DynTrans;\n\ use base qw/Exporter/;\n\ our @EXPORT;\n\ BEGIN { @EXPORT = qw/&fortytwo/; }\n\ sub fortytwo { 42; }\n\ 1;\n\ package main;\n\ DynTrans->import;"; int main(int argc, char **argv, char **env) { STRLEN n_a; char *embedding[] = { "", "-e", "0" }; pi = perl_alloc(); perl_construct(pi); perl_parse(pi, NULL, 3, embedding, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(pi); eval_pv(moduletext, TRUE); /* ~ use DynTrans; */ eval_pv("$a = fortytwo();", TRUE); /* its function is available */ printf("EXPECT: a = 42\nGOT: a = %d\n", SvIV(get_sv("a", FALSE) +)); perl_destruct(pi); perl_free(pi); return 0; }
OUTPUT: EXPECT: a = 42 GOT: a = 42

In reply to Re: Extending an embedded Perl interpreter without a .pm file by benizi
in thread Extending an embedded Perl interpreter without a .pm file by ShodadVenturi

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.