Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

perlxs documentation

by hotshot (Prior)
on May 26, 2005 at 09:28 UTC ( #460590=perlquestion: print w/replies, xml ) Need Help??

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.


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,

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://460590]
Approved by Joost
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2023-06-07 13:38 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.