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$
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.