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

hi ppl, i have an c++ application and i have embedded perl in it. and i want to use c functions of that applications in my perl files, so when i parse the pl scripts when the file encounters the functions it should pass the arguments/call the functions and perform neccessary action in the side application a fecth the result and display or white to file what ever, when i mean to say in simple words i should he using those functions from perl scripts with out creating any dll/c extentions

Replies are listed 'Best First'.
Re: c fucntions w/o using c extentions
by diotalevi (Canon) on Jan 06, 2005 at 06:34 UTC

    The following is what I told opensourcer in the chatterbox edited slightly.

    So you embedded perl inside your C++ application and wish to make C calls from perl? This is covered in perlembed: Perl ways to embed perl in your C and C++ application, perlxstut: Perl XS tutorial, perlxs: Perl XS application programming interface, . There are things you can do to make your DLL creation mostly implicit like Inline::C but in the end, you do end up with a DLL. That is, for the part where you call the C from perl. All the normal approaches anyway.

    I suppose that since you're already in C++ when calling perl you might be able to get perl to use functions from within the executable. That's probably pretty untraveled. Your best bet is to read the available documentation and try some things out.

    Well, that it is my opinion that it is relatively untraveled. It sounds like it should be relatively straightforward once you've handled the embedding. Simon Cozens also has the Extending and embedding perl book on this. You'll probably get better help if you can come up with more specific questions after actually working with this. I'll at least encourage you by saying it sounds like it should be very possible.

Re: c fucntions w/o using c extentions
by demerphq (Chancellor) on Jan 06, 2005 at 09:49 UTC

    I think you will have to produce XS wrapper for the routines you want available in your perl code. There are tools to bulk create these wrappers: as diotalevi said there is Inline::C and I think that you can also use h2xs. Basically what these tools will do is produce XS stubs and the required code to register them into the appropriate Perl package space etc. AFAIK this is pretty much the same whether Perl is embedded or not.

    ---
    demerphq

Re: c fucntions w/o using c extentions
by Joost (Canon) on Jan 06, 2005 at 20:13 UTC
Re: c fucntions w/o using c extentions
by Tuppence (Pilgrim) on Jan 07, 2005 at 14:19 UTC

    Try SWIG. It will generate the neccesary XS for you, so you can call your C++ functions and object methods from within perl. This doesn't quite fit with not creating any dll/c extensions, but AFAIK that isn't possible. And SWIG at least makes creating the darn things easy.