You should take a look at
perlcall,
perlapi and
perlguts.
But since I was working on something similar for PLJava and PLDelphi, here's some snippets with some extra comments:
char*
EmbedPerl_call_args( char* code , char* arg1 ) {
// dTHX enables multiple Perl interpreters for the API functions:
dTHX ;
// Enables the call of sub from C (handles @_ and returned values):
dSP ;
char *tmp , *retval ;
SV* ret ;
// Enable the call of the function from multiple native threads:
PERL_SET_CONTEXT(PL_curinterp) ;
// enter and save tmp to enable call to perl function (handles @_ fo
+r us):
ENTER ;
SAVETMPS ;
// Enable the insertion of arguments to send to the sub call:
PUSHMARK(SP) ;
// add arg1 as a new SV in the arguments (@_):
XPUSHs(sv_2mortal(newSVpv(arg1, strlen(arg1))));
PUTBACK ;
// call the sub:
call_pv(code , G_SCALAR);
// refresh returneds to get returned value:
SPAGAIN ;
// get the SV returned (SCALAR context):
ret = POPs ;
// tmp will point to the internal char* of the SCALAR, so,
// when we clean the scalar we clean tmp, and can't return it...
tmp = SvPV(ret , len) ;
// ... so, we copy tmp to retval to can return the char* after cle
+an tmps:
retval = malloc(sizeof(char) * (len+1)) ;
Copy(tmp, retval, len, char);
// add end of char*:
retval[len] = '\0';
// clean tmps and return to the previous state:
FREETMPS ;
LEAVE ;
// the returned value:
return retval ;
}
You also can take a look at the sources of
LibZip, at ./myldr/ and ./myldr/zlib-src/main.c to see sources of an executable that do that and for a MakeFile.PL that compile automatically your app on Linux and Windows.
Enjoy!
Graciliano M. P.
"Creativity is the expression of the liberty".