Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Embedding Perl in C -- access to builtins?

by ariels (Curate)
on May 31, 2002 at 08:21 UTC ( #170651=note: print w/ replies, xml ) Need Help??


in reply to Embedding Perl in C -- access to builtins?

When embedding C in Perl, you also need to access perl's builtins. So (confusingly for someone going in your direction) perlguts holds the keys to handling xVs.


Comment on Re: Embedding Perl in C -- access to builtins?
Re: Re: Embedding Perl in C -- access to builtins?
by premchai21 (Curate) on May 31, 2002 at 08:49 UTC

    Thank you. This helps muchly; however, there are still many perlfuncs which I do not seem to be able to directly access through those functions alone. For instance, I have an SV *, and wish to reverse() it. What do I do? I imagine it to be possible to write a package (in Perl) that is loaded by the C program's perl instance at runtime, and which is used as a proxy to the core functions, but for some functions I'd have to fiddle with the arguments rather inefficiently. Are there better ways?

      I don't know about better ... but you could create a c function that evaluates strings (ala perlembed):
      #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; SV* my_eval_sv(SV *sv, I32 croak_on_error) { dSP; SV* retval; STRLEN n_a; PUSHMARK(SP); eval_sv(sv, G_SCALAR); SPAGAIN; retval = POPs; PUTBACK; if (croak_on_error && SvTRUE(ERRSV)) croak(SvPVx(ERRSV, n_a)); return retval; } main (int argc, char **argv, char **env) { STRLEN n_a; char *embedding[] = { "", "-e", "0" }; SV *var, *cmd; my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); var = newSVpvf( "%s", "ybred" ); printf( "var is %s\n", SvPV(var,n_a) ); cmd = newSVpvf( "reverse('%s');", SvPV(var,n_a) ); var = my_eval_sv( cmd, TRUE ); printf( "var is %s\n", SvPV(var,n_a) ); perl_destruct(my_perl); perl_free(my_perl); }

      -derby

        That's about the same as the proxy module I was talking about, but with more stuff in C and less in Perl. You still run into problems with things like (for instance) splice.

        And your particular example doesn't work if the input scalar contains any single quotes... though that could be fixed with a substitution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://170651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-10-26 00:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls