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

Hey perl monks, I just got a book on Exending and Embedding.I just don't understand what perl api is.Would all my C code with my perl code would look like this <perl.h> that if i am right.

Replies are listed 'Best First'.
Re: Perl 5 API
by Joost (Canon) on Mar 25, 2007 at 01:13 UTC
    Hey perl monks, I just got a book on Exending and Embedding.
    Would that be this book?

    I just don't understand what perl api is
    The perl API (Application Program Interface) is the interface to use when talking to the perl interpreter from C/C++ and possibly other languages.

    Would all my C code with my perl code would look like this <perl.h> that if i am right.
    There's a little more to it that that - if I'm even understanding what you're asking - if you're asking anything. In other words, you're not making yourself very clear.

    If you want to embed a perl interpreter to your (C/C++) programs take a look at "Adding a perl interpreter to your C program" in perlembed.

    If you want to add C code to your perl programs take a look at perlxs.

    Or, you know, read that book you've got.

      Yes that the book and thanks for the help:)
Re: Perl 5 API
by grinder (Bishop) on Mar 25, 2007 at 12:43 UTC

    The Perl API is the list of public functions that can be called from your own C code, into the Perl interpreter implementation.

    The exact documentation perlapi is generated from the source code itself, and varies from one version to another. By and large, the core developers take care to ensure that an existing public function will continue to exist in all future implementation. This is often achieved by a using a macro as a shim over a new-style function.

    Your C code will always need to include two header files:

    • EXTERN.h
    • perl.h

    The first header file is rather small, and is used to set up how to access the global interepreter variables in a cross-platform compiler-independent manner, when you're outside the interpreter.

    The second header is good reading. It gives you a clear insight into what's available and how things are laid out.

    The C-level calling convention of Perl functions is really quite horrific, and not something you want to do with your bare hands. To that end, a lot of the details are swept under the rug by using what is known as XS, which is a sort of meta-C language. To use this, you want to include a third file:

    • XSUB.h

    If you're just starting out, XS is the way to go. One thing that helps as much as reading books, or man pages such as perlxs or perlxstut (both, sadly, beginning to show their age) is to read some source code. Have a look at some source code to modules that contain XS components, and see how they work.

    Tip o' the hat to fenLisesi for spotting a typo.

    • another intruder with the mooring in the heart of the Perl

Re: Perl 5 API
by ambrus (Abbot) on Mar 25, 2007 at 09:06 UTC

    You should probably choose an interpreted language that's easier to embed than perl, unless you specially need perl for some reason.

    Here are some options. Python is said to be easy to embed, but I've never really looked at it, so I don't know. Ruby is easy to embed, but its API documentation is not nearly complete. Lua is also easy to embed, and its documentation is quite comprehensive. You need to take a mind leap to understand how the lua api functions work (namely that you can never manipulate pointers to lua objects directly), but the irc community is eager to help. MzScheme has a complete api documentation, but the interpreted language itself you might not like. There are lots of other interpreters I didn't even mention, so google or something if you like none of these.

      If i am right i could do it like this
      #include "EXTERN.h" #include "perl.h" Package name code goes here.

      20070404 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

        Do what, exactly? We are not mind readers, you know.

        What you're describing looks vaguely like the start of an XS file.

        Do you know any C? If not, get yourself an good introduction text. Personally, I've always liked The Joy of C, but there are probably plenty of tutorials on the web.

        Also, I suggest you read chapter 2 of the extending and embedding perl book. It should clear up a lot of your confusion.