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

Ok, this one is a long shot, but i figure if anyone would know or have some sly tricks up their sleeves, it would be the monks....

Short version:
Is there a way to dynamically include perl code into a C program at runtime?

Long version:
i'm creating a daemon and originally planned to use shared libraries for dynamic functionality (depending on a config file, it would either load library A to interface with the mysql database, or library B to interface with a special format of text file, etc). This daemon would have some special characteristics that only a persistant daemon would run. I was hoping to be able to use pre-existing code to base the dynamic libs on...

Problem is, when i first wrote these programs, i wrote one (the file parser) in C and another (the mysql interface) in perl (using DBI). Rather than rewrite the perl code in C, i decided to experiment - i tried perlcc, with no avail (creating shared libraries is aparently disabled) I read up on Calling Perl from C Programs as well, but that appeared to only be able to compile in perl statically to the executable. So my question is this: is there a way of creating an equivilant system to loading a .so into a C program, instead using perl? Or am i stuck having to either rewrite it in C or exec the perl program as a seperate executable (not desirable)?

Replies are listed 'Best First'.
Re: including perl dynamically into C
by mpeppler (Vicar) on Nov 05, 2002 at 16:36 UTC
    It's certainly feasible to create a .so file that has an embedded perl interpreter, and load that .so file into your executable if it is needed.

    You need to read up on dlopen() (assuming that your system uses dlopen() for this - such as linux or solaris), and of course read the perlembed document.

    You can find out what link options to use by running

    perl -MExtUtils::Embed -e ldopts
    Having said all that - is there any reason why you can't just link everything together?

    Michael

Re: including perl dynamically into C
by broquaint (Abbot) on Nov 05, 2002 at 16:37 UTC
    Or you could the other way and embed C in perl with the marvellous Inline::C. Or back again and embed perl in C with another fantastical creation from Brian Ingerson Inline::CPR.
    HTH

    _________
    broquaint