http://www.perlmonks.org?node_id=460590

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

Hi all,

I would like to know of a way to call existing C library functions from Perl, so I tried reading a little on perlxs (is there any other way?).
The thing is I'm working with perl 5.8.0 and all the examples programs speak of very old perl version and not competible with my version, so I can train the subject.
Is there a place I can get updated information on the subject.

Thanks

Replies are listed 'Best First'.
Re: perlxs documentation
by Joost (Canon) on May 26, 2005 at 09:36 UTC
      All the examples i've tried from perlxs, perlxstut, perlapi, perlguts and perlembed have been up-to-date for me

      I tried running the example of Mytest that is given in perlxstut, the code generated for me was different than the onw they're talking about in the tutorial and I couldn't make it work.
        I hope you're refering to your local copy of perlxstut - the copies on perlmonks are out of date. But indeed, the files generated with perl 5.8.6 are slightly longer and have more options and comments in them than the docs show, though it's not too spectacular.

        I just tried the first example (adding the hello world function to the xs file) and it works fine for me. Here's what my Mytest.xs looks like (no changes required to any other file) update: I mean: just do what the tutorial says, and ignore small differences in the generated files compared to the docs:

        #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = Mytest PACKAGE = Mytest void hello() CODE: printf("Hello, world!\n");
        and the test script:
        #!/usr/local/bin/perl use ExtUtils::testlib; use Mytest; Mytest::hello(); __OUTPUT__ Hello, world!
        You should probably remember that XS is whitespace sensitive, especially with regards to the function prototypes.

        HTH, Joost.

Re: perlxs documentation
by Zaxo (Archbishop) on May 26, 2005 at 10:00 UTC

    Another, somewhat dated, way is to produce .ph files from the library's C headers and use the syscall function. Details in that perldoc.

    I think that largely has been replaced by XS because with XS you can tune the library calls to have more perlish calling conventions.

    After Compline,
    Zaxo

Re: perlxs documentation
by zentara (Archbishop) on May 26, 2005 at 11:12 UTC
    Here is an example I have from unknown origin (probably perl.inline newsgroup) for using Inline::C
    #!/usr/bin/perl # Suppose libyourlib contains a function called 'multiply' which takes + 2 # arguments of type int and returns a int. You could access it with # Inline::C as follows: use Inline C => Config => LIBS => '-L/path/to/the_static_lib -lyourlib'; use Inline C => <<'EOC'; #include "yourlib.h" int wrap_multiply(int a, int b) { return multiply(a,b); } EOC my $x = 17; my $y = 123; print wrap_multiply($x, $y); __END__ # And, so long as libyourlib.so was able to be found, that Inline scri +pt # should work fine (typing errors aside :-).

    I'm not really a human, but I play one on earth. flash japh