Here is something to get you started and how to make END blocks work (just slightly modified from a example in perlembed):

#include <EXTERN.h> #include <perl.h> /* * embedex.c * based on https://perldoc.perl.org/perlembed.html * slightly modified by bliako for https://perlmonks.org/?node_id=1229 +069 * actually, bliako just inserted the perl code. */ static PerlInterpreter *my_perl; 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 ); perl_parse(my_perl, NULL, 3, embedding, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(my_perl); eval_pv("BEGIN{ print q(in BEGIN\n); } print q(running\n); END { +print q(in END\n); }; 1;", TRUE); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

You need to compile the above using CC, CFLAGS and LDFLAGS best supplied by Perl itself via Config and ExtUtils::Embed like so (assuming bash shell):

Update: Just to clarify that the above is about embedding a perl interpreter inside a C program. The interpreter is then used to execute a perl script. This has nothing to do with taking a Perl script and converting it into C code. Or converting a Perl script into assembly. Or converting a Perl optree to C code or assembly.

$(perl -MConfig -e 'print $Config{cc}') embedex.c $(perl -MExtUtils::E +mbed -e ccopts -e ldopts) -o embedex
$ ./embedex in BEGIN running in END

Regarding detecting when a perl script is run under a debugger, read perlvar

bw, bliako


In reply to Re: Embed perl problem by bliako
in thread Embed perl problem by Noves Castro

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.