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

I am attempting to write a C program that uses libperl and its associated structures (xV in particular). I would like to access Perl's built-in functions (i.e. those listed in perlfunc), but I haven't been able to find any documentation on doing so. I've read perlapi and perlembed, among other docs, but haven't found much of help, though it's likely I've missed something. It seems as if I might have to manipulate the/an opcode tree (just based on output from B::C), but that seems frightening, mostly because I really don't know how, and can't find a good reference.

Are there any suggestions from the kind monks?

  • Comment on Embedding Perl in C -- access to builtins?

Replies are listed 'Best First'.
Re: Embedding Perl in C -- access to builtins?
by Matts (Deacon) on May 31, 2002 at 12:05 UTC
    Some can be called via perlcall. See specifically perl_call_pv().

      That sounds like a good idea.

      $ ./reverse
      var is narf!
      Segmentation fault


      cat reverse.c

      #include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; static SV * do_reverse(x) SV *x; { dSP; int count; SV *var; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs(sv_mortalcopy(x)); PUTBACK; count = perl_call_pv("CORE::reverse", G_SCALAR); SPAGAIN; if (count != 1) croak("Aieee!"); var = POPs; PUTBACK; FREETMPS; LEAVE; } main (int argc, char **argv, char **env) { STRLEN len; SV *var; my_perl = perl_alloc(); perl_construct(my_perl); var = newSVpvf("%s", "narf!"); printf("var is %s\n", SvPV(var, len)); var = do_reverse(sv_2mortal(var)); printf("var is %s\n", SvPV(var, len)); perl_destruct(my_perl); perl_free(my_perl); }

      According to gdb, it's segfaulting on the perl_call_pv. What am I doing wrong?

      (Running perl, v5.6.1, built for i686-linux-multi.)

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

    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.

      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); }


Re: Embedding Perl in C -- access to builtins?
by Zaxo (Archbishop) on May 31, 2002 at 08:03 UTC

    I've never done that, so keep the salt handy. You might take a look at XSUB.h, embed.h, and the output of 'nm libperl.a'. Those can be found in perl's lib/$version/$arch/CORE.

    After Compline,