in reply to Re^2: perl c++ perlembed question
in thread perl c++ perlembed question

I'm not exactly sure of your requirements - you would be better off to provide some simple/minimalist test program that demonstrates what you're attempting to achieve. (Since you have not yet worked out how to achieve what you're after, that test script would presumably not work - but if it gave us some hints regarding the essence of your aims, that might help.)

Here's a basic, contrived example of how the return values of C functions (namely, the foo() and bar() functions) could be fed into the embedded perl process. It's just a slight modification of the 'power.c' example from perlembed:
#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; static void PerlPower(int a, int b) { dSP; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_2mortal(newSViv(a))); XPUSHs(sv_2mortal(newSViv(b))); PUTBACK; call_pv("expo", G_SCALAR); SPAGAIN; printf ("%d to the %dth power is %d.\n", a, b, POPi); PUTBACK; FREETMPS; LEAVE; } int foo(void) { unsigned long t = (unsigned)time( NULL ); return (t % 5) + 3; } int bar(void) { unsigned long t = (unsigned)time( NULL ); return (t % 4) + 2; } int main (int argc, char **argv, char **env) { char *my_argv[] = { "", "power.pl" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(my_perl); PerlPower(foo(), bar()); /* foo() ** bar() */ perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
For that program to work, 'power.pl' needs to be also in the cwd - and needs to contain the following:
sub expo { my ($a, $b) = @_; return $a ** $b; }
Cheers,
Rob

Replies are listed 'Best First'.
Re^4: perl c++ perlembed question
by eth0nic (Initiate) on Jun 27, 2009 at 16:28 UTC
    i cant provide a test script ... all i got is what youve already postet ... a working embedded perl script that IS NOT ABLE to call functions from the part of the programm/code that embedded the actual perl script :-)

    i got a c++ project that defines a function

    foobar()

    and also embeds a perl script larry.pl

    ok?

    now the c++ code will embed perl and run larry.pl ... now i want larry.pl to be able to access the foobar() function that was declared in the c++ code that actually embedded the larry.pl script. NOT any other laying around c++/c code r module.
Re^4: perl c++ perlembed question
by Anonymous Monk on Jun 27, 2009 at 16:08 UTC
    wow, that was "easy" :-) thanks a ton rob! i couldnt find ANY example for calling back into c++ from an embedded perl script anywhere ... thanks again :-) you made my day
Re^4: perl c++ perlembed question
by eth0nic (Initiate) on Jun 27, 2009 at 16:11 UTC
    hm but that also does not call a function from the c++ code from within the power.pl script?

    i wanted
    c++ with a func bla() and an embedded perl interpreter
    this c++ programm is executed and running
    it embeds a perl interpreter that runs foo.pl invoked with some param
    foo.pl runs and executes bla() from the c++ script and gets some return value from the bla() func
    foo.pl returns something to the c++ program
    ...
      that also does not call a function from the c++ code from within the power.pl script

      That's correct - sub expo is simply being passed values that come from the C code (subs foo and bar).

      foo.pl runs and executes bla() from the c++ script and gets some return value from the bla() func

      That's the tricky bit ... well, "impossible" rather than "tricky", one suspects. I don't know how foo.pl could run bla() directly.

      I also don't know why foo.pl would *need* to run bla() directly, given that bla()'s return value could be passed to foo.pl as an argument (as per the power.pl example already supplied). If the need to have foo.pl call bla() is a non-negotiable requirement, then I can't help at all with that aspect.
      UPDATE: I now think I *can* envisage circumstances where it would be nice if foo.pl could call bla() directly - but I still have no idea how that could be achieved.

      Thanks for an interesting question, btw. (I learned something in trying to answer it.)

      Cheers,
      Rob