[% setvar title Replace XS with the C module as the standard way to extend Perl. %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Replace XS with the Inline module as the standard way to extend Perl.

VERSION

  Maintainer: Brian Ingerson <brian@ingerson.com>
  Date: 21 Sep 2000
  Mailing List: perl6-language@perl.org
  Number: 270
  Version: 1
  Status: Developing

REPLACES

RFC 61 - Interfaces for linking C objects into perlsubs

ABSTRACT

Extending Perl with XS is too hard. First, there is a hefty learning curve for even simple extensions. Also, the resulting code gets spread out over several files, making it hard to maintain.

In the spirit of Perl itself, Inline.pm makes extending Perl easy for easy things, and possible for harder things. A Perl programmer can write their first Inline extension in minutes. They can learn more difficult maneuvers as needed. All of the extension code can be in the same file as the Perl script or module.

This one-liner is a complete perl extension:

    perl -e 'print add(2,2);use Inline C=>"int add(int x,int y){return x+y;}"'

The first time you run it, you'll notice a pause (compiling). Subsequent runs are lightning fast, as long as the C component isn't modified.

DESCRIPTION

Inline.pm is basically a user friendly abstraction over XS. You feed it a snippet of (C) source and it performs the following steps:

    1) Determine if the code snippet has already be compiled. 
       If so, goto 5.
    2) Parse function definitions to determine how code should bind to Perl.
    3) Generate XS glue code.
    4) Build the extension and install it in some known place.
    5) DynaLoader the extension.

If the extension is a user script or one-liner, the extension will get built and installed in a place that the user has access to. Inline chooses a reasonable default. The default can easily be over-ridden.

If the extension is part of a distributed (ie CPAN) module, the code gets compiled during "make test" and permanently installed in the "installsitearch" during "make install".

Inline is intended to replace 80-90% of the current functionality of XS. Although it does not need to be built over XS, doing so makes Inline more robust, helps towards backwards compatability, and provides an easy "out" if a project grows to exceed Inline's capabilities.

In perl6, something like XS should still exist, but just as a foundation for Inline. Savvy hackers could defeat Inline and write glue code themselves, but this would not be the standard.

IMPLEMENTATION

All of this is currently functional in Inline v0.25 (on CPAN now). Inline seems to work on any machine that has access to the same environment that was used to build Perl itself. Success has been achieved on platforms including MSWin32 and most *nixes.

Version 0.25 provides bindings to the following types: int, long, double, char *, SV *. (For anything else the user must pass the argument as a SV * and do their own type conversion.) There is also support for passing and returning lists.

Version 0.30 (not yet released) has no default types. It gets all of its types from XS typemap files. These files are parsed for their types, which are dumped into the grammar to parse C. Since Perl comes with a generic typemap file, this is used as the default. It contains all the types listed above and more.

This allows XS programmers to use their old typemaps when switching to Inline. It also allows other modules (like Event.pm) which have an XS API, to publish that API to Inline seamlessly, using a syntax like "use Inline with => 'Event';"

Version 0.30 will also support the following syntax:

    use Inline;
    print add(2, 4);
    __END__

    =pod

    blah blah blah

    =cut

    __C__
    int add(int x, int y) {
      return x + y;
    }

If so desired, perl6 might be able to simply recognize the __C__ marker, and not require the use Inline; at all. The code would look like:

    print add(2, 4);

    __C__
    int add(int x, int y) {
      return x + y;
    }

    __END__

    =pod

    blah blah blah

    =cut

REFERENCES

RFC 61: Interfaces for linking C objects into perlsubs

Inline.pm documentation and tutorial:

search.cpan.org

search.cpan.org

search.cpan.org