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

Dear Friends , I am compltely into c/c++ programming, currently I have written a c funtion in a .so file which looks like the one given below
char *abc() { char *p; p="Hello world"; return p; }
if this function is executed via a.c program(a.out as executable) the output is perfect. I want to have the result displayed onto the browser via the perl in an htm file.

so according to the module whenever the submit button is pressed at the client side, I want to execute the function from the .so file via the perl file.

I looked for help in goole.com, but all the results that I am getting is only for NS(netscape server) and Livewire. I am using apache under RHL 7.2 so kindly suggest me. but i have never worked on any scripting languages. so I would appreciate if some guide lines cud be given to me Its very urgent. Thank you for your patience

Transplanted from Tutorials, better title - dvergin 2002-08-29

Replies are listed 'Best First'.
Re: Need a Help
by joe++ (Friar) on Oct 23, 2002 at 18:29 UTC
    You could have a look at Inline::C, but why not just spit out a line "Content-type: text/html", followed by two CR and LF chars and then just printf the rest to STDOUT? This way you could directly run your program as barebones CGI...

    --
    Cheers, Joe

Re: Need a Help - calling C program from Perl
by derby (Abbot) on Oct 23, 2002 at 18:45 UTC
    Two options:

    one: make an executable and then within your cgi script use one of the myriad of ways to execute the program (system,fork/exec,qx).

    or

    two: wrap the library (.so file) within XS or Inline::C or SWIG.

    -derby

Re: Need a Help - calling C program from Perl
by chromatic (Archbishop) on Oct 23, 2002 at 23:13 UTC

    This is somewhat off topic and my C skills aren't exceptionally sharp, but aren't you asking for trouble by returning a pointer allocated within your function?

Re: Need a Help - calling C program from Perl
by true (Pilgrim) on Oct 24, 2002 at 08:48 UTC
    I tried this. I was curious.
    I downloaded the module from cpan.
    cpan> install Inline::C
    during install it asked me for my compiler.
    i told it 'cc'. It completed install.
    Got a sample from here, CPAN Inline::C
    and added a Content-type header. I noticed that 'use strict;' did
    not work with this module on my RHL6.2.8.
    Besides that, the following test script
    worked great in the shell and a browser.
    #!/usr/bin/perl -w ##use strict; use Inline C; greet('Content-type:text/html'); greet('Hello from c via CGI'); __END__ __C__ void greet(SV* sv_name) { printf("%s\n\n", SvPV(sv_name, PL_na)); }

    Here's the output from my error logs when strict was on.
    Bareword "C" not allowed while "strict subs" in use at c.pl line 3. BEGIN not safe after errors--compilation aborted at c.pl line 3.
    The CPAN link above has a lot of information about useage.
    I tried your original .so example but no dice.
    But the CPAN link talks about linking your .so,
    read more cause i don't know squat.
    Was fun to try though. BTW i did this in perl5.005

      use Inline C;

      should be

      use Inline::C;?

      That would allow you to use strict.


      Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      If you read the documentation or try to run a test script. Use 'Inline C' is the only way to include this. You cannot call this with 'Inline::C' but 'Inline C'. Check the Inline C Documentation.
      Here's a quote from the cpan author.
      You never actually use Inline::C directly. It is just a support module + for using Inline.pm with C. So the usage is always: use Inline C => +...;
      If you know of a way to get around this i would love to hear it.

      edited:tone :)

        Think of "use" as a function call, with specialized first argument, which is a bareword representing a name, then followed by arbitrary parameters:

        use Module ( $arg1, $arg2, ... );

        So now, think of this:

        use Inline C;

        The first argument ("Inline") is special, so that's ok, but what about the second argument, "C"? It's a bareword. That should raise the flag at which point you say, "Oh, I need to quote it!"

        use Inline 'C';

        Ah, but why did it work when it was

        use Inline C => ...;

        you say? The answer is simple. It's the same reason why the first example below works, but not the second one:

        use strict; foo( FOOBAR => 'baz' ); foo( FOOBAR, 'baz' );

        Yep, the "key" is automatically stringified upon encountering "=>". So when you look at the Inline example, you can clearly see that that's using this stringification trick.

        Just for the record, from perlop:

        The => digraph is mostly just a synonym for the comma operator. It's useful for documenting arguments that come in pairs. As of release 5.001, it also forces any word to the left of it to be interpreted as a string.

        P.S. - you really should reply to the post that you refer to in your node, otherwise the poster to whom you're replying to may not notice