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

I'm struggling to learn how to embed the perl interpreter into c. I've pieced together the following test but I don't understand what I'm doing wrong. I'm clearly not passing arguments from perl back to c correctly, as the string that I expect to get back is missing the first 4 characters (and those chars are being replaced by, what looks to me like, garbage).

Will anyone please let me know what I'm doing wrong?

thanks

-- Jonathan


jrs@madrona emperl$ ls -la

total 24 drwxrwxr-x 2 jrs jrs 4096 Jun 13 08:14 . drwx------ 80 jrs jrs 12288 Jun 12 19:54 .. -rw-rw-r-- 1 jrs jrs 756 Jun 13 08:07 main.c -rw-rw-r-- 1 jrs jrs 127 Jun 13 08:14 test.pl

jrs@madrona emperl$ cat test.pl
#!/usr/bin/perl -w use strict; sub reverse_string { my ($string) = @_; my $rev = reverse $string; return $rev; }

jrs@madrona emperl$ cat main.c
#include <stdio.h> #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { char in_string[] = "abcdefg"; char *out_string; char *embedding[] = { "", "test.pl" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, 2, embedding, NULL); dSP; ENTER; SAVETMPS; PUSHMARK(sp); XPUSHs(sv_2mortal(newSVpv(in_string, 0))); PUTBACK; perl_call_pv("reverse_string", G_SCALAR); SPAGAIN; out_string = POPp; PUTBACK; FREETMPS; LEAVE; printf ("reverse %s -> %s\n", in_string, out_string); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

jrs@madrona emperl$ cc -o test main.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
main.c: In function `main':
main.c:17: warning: ISO C90 forbids mixed declarations and code
jrs@madrona emperl$ ./test
reverse abcdefg -> x�cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x� cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x� cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x�cba
jrs@madrona emperl$ ./test
reverse abcdefg -> xR
jrs@madrona emperl$ ./test
reverse abcdefg -> x"cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x�cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x�cba
jrs@madrona emperl$ ./test
reverse abcdefg -> xb� cba
jrs@madrona emperl$ ./test
reverse abcdefg -> x�cba
jrs@madrona emperl$

Replies are listed 'Best First'.
Re: embedding perl in c?
by almut (Canon) on Jun 13, 2008 at 16:38 UTC

    FREETMPS is cleaning up any temporary variables/values, which includes the return values of the Perl subroutine, so you have to copy your out_string in case you need it beyond FREETMPS. (Put the printf... statement immediately after out_string = POPp; and you'll see that things are working fine.)

    BTW, POPp is deprecated — use POPpx instead, which also gives you the length of the string.

      That was it. Thanks a lot!
      -- Jonathan