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

Brian Ingerson brian at ingerson.com
Thu Sep 21 14:52:27 CDT 2000

=head1 TITLE

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

=head1 VERSION

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


RFC 61 - Interfaces for linking C objects into perlsubs


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, C<Inline.pm> makes extending Perl easy for
easy things, and possible for harder things. A Perl programmer can write
their first C<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

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.


C<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
    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. C<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".

C<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
C<Inline> more robust, helps towards backwards compatability, and
provides an easy "out" if a project grows to exceed C<Inline's>

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


All of this is currently functional in C<Inline> v0.25 (on CPAN now).
C<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: C<int>, C<long>,
C<double>, C<char *>, C<SV *>. (For anything else the user must pass the
argument as a C<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 C<typemap> files. These files are parsed for their types,
which are dumped into the grammar to parse C. Since Perl comes with a
generic C<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
C<Inline>. It also allows other modules (like Event.pm) which have an XS
API, to publish that API to C<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);


    blah blah blah


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

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

    print add(2, 4);

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



    blah blah blah




 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/

More information about the spug-list mailing list