This is the first time I have done anything in C with perl, but this seems to work:
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *foo;
main(int argc, char **argv, char **env) {
char * args[] = { NULL, NULL};
args[0] = "geh\n";
foo = perl_alloc();
perl_construct(foo);
perl_parse(foo, NULL, argc, argv, env);
perl_call_argv("printme", G_DISCARD, args);
perl_destruct(foo);
perl_free(foo);
}
Then for my script I used:
printme();
sub printme { print $_[0] };
The results:
prompt% myperl script.pl
geh
So one of your problems was the last argument in
perl_call_argv. You need to pass it an array of (char *)
or a (char **) not just a single (char*).
Also the last element in that char * array apparently needs to be NULL.
So that is why I created a two element array, but only populated
the first element.
For more goods see
perlcall and
perlembed
I hope this helps get you started. I am glad you asked this question. I was also curious about the perl/C interface.
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.