in reply to Perl API with C

You are missing a perl interpreter.

To learn how to embed perl in your C program, check out perlembed. Here is interp.c from the 5.8.0 docs:

#include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ int main(int argc, char **argv, char **env) { my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); }
This would then be compiled with a statement like
% cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
It is a bit of a learning curve to figure all this out, but people have embedded perl in their C programs as a built-in scripting language. It is less popular than one might think, however, because it is so easy for perl to interact with standalone C programs.

-Mark

Replies are listed 'Best First'.
Re: Re: Perl API with C
by bakunin (Scribe) on Apr 17, 2004 at 20:04 UTC
    Thanks kvale, but I don't want to embed anything, I just want to use Perl API's C functions within a C program (if it's possible). :))
      Au contraire. You do want to embed something - a Perl interpreter!

      The Perl API is an interface between C and a Perl interpreter. For instance when you try to create a newAV, you needs to AvALLOC it, which allocation assumes that some memory arenas have already been initialized and exist so that you have somewhere to allocate the basic data structure. Which is something that a Perl interpreter does when you start it up. Without a Perl interpreter you are going to do something silly like trying to follow an unitialized value of PL_sv_root and crash after following a null pointer.

      So you can either figure out everything that a Perl interpreter initializes coming into existence (which is basically constructing a Perl interpreter by hand, and WILL break between releases of Perl), or else you can create a Perl interpreter.

      Now hie thee to perlembed as suggested. And note that the first section is about who needs to read perlembed. Given what you are doing, it is appropriate for you.

        Hmmmm... Thanks! Will do!