Bidirectional communication between Perl and C

Discussion in 'Perl Misc' started by fjliu2004@gmail.com, Jan 6, 2006.

  1. Guest

    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
     
    , Jan 6, 2006
    #1
    1. Advertising

  2. wrote:

    > 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__
     
    Brian McCauley, Jan 7, 2006
    #2
    1. Advertising

  3. wrote:
    > 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
     
    Mark Clements, Jan 7, 2006
    #3
  4. In article <>,
    wrote:

    > 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.

    --
    DeeDee, don't press that button! DeeDee! NO! Dee...
     
    Michael Vilain, Jan 7, 2006
    #4
  5. Dr.Ruud Guest

    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"?

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jan 7, 2006
    #5
  6. Guest

    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
     
    , Jan 10, 2006
    #6
  7. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 10, 2006
    #7
  8. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 10, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Bolling
    Replies:
    0
    Views:
    512
    Bryan Bolling
    Jul 16, 2003
  2. Eric Veltman
    Replies:
    3
    Views:
    530
    Carl Prothman [MVP]
    Dec 26, 2003
  3. J Rice
    Replies:
    6
    Views:
    726
    J Rice
    Mar 11, 2006
  4. Manfred Balik
    Replies:
    12
    Views:
    6,684
    Marc Guardiani
    Sep 10, 2006
  5. Giojo
    Replies:
    2
    Views:
    144
    Bart Lateur
    Dec 13, 2004
Loading...

Share This Page