[sf-perl] Calling perl from C and back again.

David Ljung Madison pm.org at daveola.com
Sat Jun 30 20:03:33 PDT 2007


I'm writing C code that calls perl (using perl_construct and it's ilk),
and I'd like to call functions back in the C code.  For example, in
pseudo-code:

==================================================
------
C Code
------

static int some_C_global;

void some_C_routine() {
  some_C_global++;
}

int main() {
  some_C_global = 41;

  PerlInterpreter *my_perl = perl_alloc();
  ..  // Setup perl and call_pv:
  call_pv("some_perl_function", G_DISCARD);
  ..

  printf("some_C_global = %d\n",some_C_global); // Is hopefully 42!
}

---------
Perl Code
---------

sub some_perl_function {
  ... # Does some perl stuff
  some_C_routine()  # Call back into the C code.
}
==================================================

I tried making a .xs for some_C_routine (using Package main) and
including that into the C code, but I still get:

Undefined subroutine &main::some_C_routine called at script.pl line 11.


Any ideas?  Or perhaps a simple example??

Thanks!



Dave

---------------------------------------------------------------------------
Dave Ljung Madison          http://GetDave.com/                415.341.5555
--- I am totally into Perl now.  I love it.  It rules with an iron fist. --


More information about the SanFrancisco-pm mailing list