in reply to perl c++ perlembed question

callbacks in XS code, Re: Correct way to return a reference in XS/embedded perl, Call C functions from Perl, pickle

Replies are listed 'Best First'.
Re^2: perl c++ perlembed question
by eth0nic (Initiate) on Jun 27, 2009 at 04:25 UTC
    thank you for your answer but that does not explain how to call a function from perl that is defined in the c++ code that embedded the perl code ... i only found "How to export functions from C++ to embedded perl. Options" http://groups.google.com/group/perl.xs/browse_thread/thread/190893fecc6bdc52
      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
        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.
        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
        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
        ...
      thank you for your answer but that does not explain how to call a function from perl that is defined in the c++ code

      I thought it did. You use Win32::API (or C::DynaLib) from perl to load/call c functions

      or you write your own c++ dispatcher function, and you make it available to perl through xs/Inline::C/pickle.

        i dont want to call JUST ANY c function ... i want to call MY OWN functions that got defined/declared in the code that after declaring those functions embedded the perl script. and i want to access those functions from within the embedded perl script

        i.e.
        blubb.cpp does 2 things ... declaring foobar() and embedding lalala.pl

        now i want to call foobar() from within the embedded lalala.pl

        i really dont know how else i should explain it ... it seems nobody understands me :-(