Bidirectional communication between Perl and C

F

fjliu2004

Hi,

I have a main program written in C, which needs to call a Perl program
to do some work, and the Perl program in turn needs to call a C
function in the main program to pass back the values it produces. Can
anyone let me know how to do this? All the examples I found in the
perlcall man pages only deal with unidirectional communication between
Perl and C, not bidirectional.

So what I want to do is the following:

main program (in C) <calls> Perl code <calls> C func (in
main program)

BTW, the communication CANNOT be done by opening a pipe. It HAS TO be
done by the (called) Perl code calling a C function in the main
program.

Thanks in advance for the help!

Frank
 
B

Brian McCauley

So what I want to do is the following:

main program (in C) <calls> Perl code <calls> C func (in
main program)

BTW, the communication CANNOT be done by opening a pipe. It HAS TO be
done by the (called) Perl code calling a C function in the main
program.

To see how to call the Perl interpreter as a library from C...

perldoc perlembed

However you may find it simpler to turn it round and make the "main"
program be the Perl one and compile your C "program" as a Perl module

All the "main" would do is call a single function written in C which
then calls back to subroutines in the Perl which in turn calls stuff in
C.

#!/usr/bin/perl
use strict;
use warnings;

# Exports &c_function and &c_main
use My::C::prog::As::A::Module;

sub some_function {
# Do stuff...
c_function();
# Do stuff...
}

c_main();

__END__
 
M

Mark Clements

Hi,

I have a main program written in C, which needs to call a Perl program
to do some work, and the Perl program in turn needs to call a C
function in the main program to pass back the values it produces. Can
anyone let me know how to do this? All the examples I found in the
perlcall man pages only deal with unidirectional communication between
Perl and C, not bidirectional.

So what I want to do is the following:

main program (in C) <calls> Perl code <calls> C func (in
main program)

BTW, the communication CANNOT be done by opening a pipe. It HAS TO be
done by the (called) Perl code calling a C function in the main
program.
I'm not entirely sure what you're after, but since pipe communication is
out of the question for some reason, you may like to check out

perldoc perlembed

Note that it is rather involved.


Mark
 
M

Michael Vilain

Hi,

I have a main program written in C, which needs to call a Perl program
to do some work, and the Perl program in turn needs to call a C
function in the main program to pass back the values it produces. Can
anyone let me know how to do this? All the examples I found in the
perlcall man pages only deal with unidirectional communication between
Perl and C, not bidirectional.

So what I want to do is the following:

main program (in C) <calls> Perl code <calls> C func (in
main program)

BTW, the communication CANNOT be done by opening a pipe. It HAS TO be
done by the (called) Perl code calling a C function in the main
program.

Thanks in advance for the help!

Frank

Given that you'd have two processes, one running your C program and one
running the perl interpeter, how would that work? The perl code doesn't
run in the context of your c program. It's forked, unless you actually
built a perl interpeter _into_ your program. So, how would it be able
to call your c function unless you built it into perl?

Take a look at a basic UNIX programming book, specifically the chapter
on Interprocess Communication. There's sockets or pipes or semiphores
or shared memory. perl has a bunch of CPAN or extension to access
shared memory (the IPC modules). Your C program would have to
coordinate use of one these methods and the perl program would have to
access whatever thing you chose via one of one of these modules:

http://search.cpan.org/search?query=IPC&mode=all

Time to roll up your sleeves and design. You might run your design by a
senior UNIX programmer before you start coding. I think it's time to
rethink your approach.
 
D

Dr.Ruud

(e-mail address removed) schreef:
I have a main program written in C, which needs to call a Perl program
to do some work, and the Perl program in turn needs to call a C
function in the main program to pass back the values it produces.

There are several standard interfaces you can use.

You can use a shared library. The C-program first registers a call-back
in a function in the library, then starts the Perl program, which does
its work and hands over the results to the same function, which collects
them and calls back the C-program.

But why not let the code that calls the Perl-program collect the results
and use them for the call of the "C function in the main program"?
 
F

fjliu2004

Dear Brian:

Thanks for the reply. How can c_main() call the Perl sub
"some_function"? Do I need to embed a Perl interpreter in my C program?
If this is the case, how can I make sure that when c_main() calls
"some_function" and c_function() is called, it will be the c_function()
in the same process as started by c_main()?

To paraphrase, what I want to do is to maintain a *single* memory image
that contains a shared library written in C that can call subroutines
in Perl and whose subroutines Perl can call.

Thanks for the help!

Frank
 
X

xhoster

Hi,

I have a main program written in C, which needs to call a Perl program
to do some work, and the Perl program in turn needs to call a C
function in the main program to pass back the values it produces.

Why not have the C program call the Perl routine, have the Perl sub
return it's return value in the normal way, and then have the C program
call the call-backs on those return values? I think that will be easier
than passing the callbacks into Perl.

Can
anyone let me know how to do this? All the examples I found in the
perlcall man pages only deal with unidirectional communication between
Perl and C, not bidirectional.

From my reading of perldoc perlcall, it is bidirectional, not
unidirectional. It is just driven from the Perl side, not driven from the C
side, as you want.

Xho
 
X

xhoster

Dear Brian:

Thanks for the reply.

Brian made two suggestions. Because you didn't quote the relevant
material, it is not exactly clear what you are responding to. I assume you
are only refering to the "inside out" part of his post and not the other
part.
How can c_main() call the Perl sub
"some_function"?

That is what perldoc perlcall tells you.
Do I need to embed a Perl interpreter in my C program?

No, what Brian recommend is a Perl program. Therefore, it already has
a perl interpreter. That "interpreter" links to your compiled C code.
If this is the case, how can I make sure that when c_main() calls
"some_function" and c_function() is called, it will be the c_function()
in the same process as started by c_main()?

There is only one process.
To paraphrase, what I want to do is to maintain a *single* memory image
that contains a shared library written in C that can call subroutines
in Perl and whose subroutines Perl can call.


use strict;
use warnings;
use Inline 'C' ;
c_main();
sub fred {
warn "I am now in fred";
back_to_c();
};
__DATA__
__C__
void c_main() {
call_pv("fred",0);
};
void back_to_c() {
printf("I am now back into C after going through Fred\n");
};

Note that I haven't checked the return value of call_pv, I don't
know if the flag I passed to it proper.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top