smack has asked for the wisdom of the Perl Monks concerning the following question:

Good day fellow monks,

I need a bit of assistance embedding a perl script into a C++ console application. I am using Microsoft Visual C++(Not by choice). I am able to embed a perl interpreter that seems to work. The following code functions properly:
#include <stdafx.h> #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main (int argc, char **argv, char **env) { STRLEN n_a; char *embedding[] = { "", "-e", "0" }; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); /** Treat $a as an integer **/ eval_pv("$a = 3; $a **= 2", TRUE); printf("a = %d\n", SvIV(get_sv("a", FALSE))); /** Treat $a as a float **/ eval_pv("$a = 3.14; $a **= 2", TRUE); printf("a = %f\n", SvNV(get_sv("a", FALSE))); /** Treat $a as a string **/ eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", T +RUE); printf("a = %s\n", SvPV(get_sv("a", FALSE), n_a)); perl_destruct(my_perl); perl_free(my_perl); return 0; }
However, I am unable to access prewritten perl functions. Here is the code I have been attempting to get to work:
#include "stdafx.h" #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; static void call_PrintUID() { dSP ; PUSHMARK(SP) ; call_pv("PrintUID", G_DISCARD|G_NOARGS) ; } int main (int argc, char **argv, char **env) { char *embedding[] = { "", "printuid.pl" }; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); call_PrintUID(); perl_destruct(my_perl); perl_free(my_perl); return 0; }

I haven't used C/C++ in a long time and was never that great at it anyways, so this is very frustrating. If anyone could help me figure out where I am going wrong I would GREATLY appreciate it. =)

Best Regards!
smack

Replies are listed 'Best First'.
Re: Embedding perl into a win32 console application
by Anonymous Monk on Oct 07, 2004 at 21:45 UTC
    You might want to take a look at the perlembed-POD (full title: 'how to embed perl in your C program')
    (If you haven't done so already)
Re: Embedding perl into a win32 console application
by kutsu (Priest) on Oct 07, 2004 at 21:52 UTC

    I assume perl_alloc(); is in perl.h which you haven't posted, so I'm not sure why it, and the other methods, aren't working. Actually, instead of writing it yourself, you might try ExtUtils::Embed.

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: Embedding perl into a win32 console application
by gmpassos (Priest) on Oct 08, 2004 at 01:42 UTC
    You should take a look at perlcall, perlapi and perlguts.

    But since I was working on something similar for PLJava and PLDelphi, here's some snippets with some extra comments:

    char* EmbedPerl_call_args( char* code , char* arg1 ) { // dTHX enables multiple Perl interpreters for the API functions: dTHX ; // Enables the call of sub from C (handles @_ and returned values): dSP ; char *tmp , *retval ; SV* ret ; // Enable the call of the function from multiple native threads: PERL_SET_CONTEXT(PL_curinterp) ; // enter and save tmp to enable call to perl function (handles @_ fo +r us): ENTER ; SAVETMPS ; // Enable the insertion of arguments to send to the sub call: PUSHMARK(SP) ; // add arg1 as a new SV in the arguments (@_): XPUSHs(sv_2mortal(newSVpv(arg1, strlen(arg1)))); PUTBACK ; // call the sub: call_pv(code , G_SCALAR); // refresh returneds to get returned value: SPAGAIN ; // get the SV returned (SCALAR context): ret = POPs ; // tmp will point to the internal char* of the SCALAR, so, // when we clean the scalar we clean tmp, and can't return it... tmp = SvPV(ret , len) ; // ... so, we copy tmp to retval to can return the char* after cle +an tmps: retval = malloc(sizeof(char) * (len+1)) ; Copy(tmp, retval, len, char); // add end of char*: retval[len] = '\0'; // clean tmps and return to the previous state: FREETMPS ; LEAVE ; // the returned value: return retval ; }
    You also can take a look at the sources of LibZip, at ./myldr/ and ./myldr/zlib-src/main.c to see sources of an executable that do that and for a MakeFile.PL that compile automatically your app on Linux and Windows.

    Enjoy!

    Graciliano M. P.
    "Creativity is the expression of the liberty".