Inline::SWIG - Write Perl in C/C++/Objective-C using SWIG. (v0.00)


 use Inline SWIG => 'int meaning() { return 42; }';
 print &meaning();
 use Inline SWIG => <<"END_CODE", SWIG_ARGS => '-c++ -shadow', CC => "c++";
 class Foo {
     int meaning() { return 42; };
 my $o = new Foo();
 print $o->meaning(),"\n";


Inline::SWIG is like Inline::C (or Inline::CPP), but uses SWIG ( to do the parsing and glue code generation.

See the Inline::C manpage, Inline,, and the Inline::SWIG homepage

I suggest using Inline::C or Inline::CPP instead of this module, unless you really want/need SWIG's glue.

SWIG's support of perl is currently (ver 1.3.15, 2002-Sep) in flux. For example, the -shadow of the ``-c++ -shadow'' idiom is required, and is used in SWIG's perl Examples, but it is no longer a documented swig command line argument. Sigh.



Inline::C configuration options (such as ``CC'') can be used. But those which control glue generation, now being done by SWIG, are ignored.

    ..., CC => 'c++';  # Use c++ instead of cc.


A string of swig command line arguments. See SWIG documentation. ``-c++'' enables C++ processing.

    ..., SWIG_ARGS => '-c++';


SWIG .i code. A SWIG_INTERFACE code string is placed in a SWIG .i file. Swig is run on it, instead of the source code.

A ``%module mumblepackage'' declaration (optional) loads the code into package mumblepackage, instead of into the package enclosing the ``use Inline SWIG'' statement.

    use Inline SWIG => '... .c code ...', SWIG_INTERFACE => '... .i code ...';
    use Inline SWIG => ' ', SWIG_INTERFACE => '... .i code ...';
    BEGIN { $header = '... .h code ...'; }
    use Inline SWIG => $header.' ', SWIG_INTERFACE => <<'END';
    ... .i code, including perhaps another copy of $header ...


    Defaults to "swig -perl5";


Using a combination of code and interface strings

If one uses both code and interface strings, which end up as separate code and interface files, one may need to have common header information. Here is one way to handle it.

The BEGIN makes the $foo_h variable available at Perl ``compile''-time, which is when the ``use Inline'' happens.

    $foo_h = <<"  END_OF_H";
    class Foo {
          int meaning();
  use Inline SWIG => <<"END_OF_CODE", SWIG_INTERFACE => <<"END_OF_I", SWIG_ARGS => '-c++ -shadow', CC => "c++";
  int Foo::meaning() { return 42; }
  %module example
  my $o = new example::Foo();
  print $o->meaning(),"\n";

Dynamic code generation

  Inline::SWIG can also be invoked at Perl "run"-time.
  use Inline;
  sub make_adder {
    my $n = shift;
    my $function_name = "f".int(rand(1000000));
    my $code = "int ${function_name}(int x) { return x + $n; }";
    Inline->bind(SWIG => $code);
    return \&$function_name;
  my $add3 = &make_adder(3);
  print $add3->(2); # -> 5


Inline::SWIG isa Inline::C. To compile code, it runs swig, edits the result, and then hands them off to Inline::C.

Inline::SWIG edits the SWIG generated mumble_wrap.c and files. It separates the naming of library boot code from the naming of packages. Thus making the library files compatible with Inline, while retaining the desired package names.

Normally, a SWIG .pm file loads the associated library. To avoid fighting with Inline, Inline::SWIG instead loads the .pm file from the library. The .pm file is renamed, its loading code removed, and a ``require'' inserted into the mumble_wrap.c initialization code.


This package is a kludge on top of two unstable codebases.

Inline::SWIG v0.00 was tested with SWIG v1.3.15, and Inline v0.43.

Inline::SWIG does regex substitutions into the SWIG's output code. And SWIG's perl support is currently (ver 1.3.15, 2002-Sep) in a state of flux. And even SWIG command line options are unstable. So new versions of SWIG are likely to break Inline::SWIG.

Inline::SWIG inherits from Inline::C, which is part of Inline. So changes to Inline and Inline::C may cause problems.

Testing has been very limited. It follows that bugs are very common.


Inline, the Inline::C manpage,


Mitchell N Charity <>


 Copyright (c) 2002, Mitchell N Charity. All Rights Reserved.
 This module is free software. It may be used, redistributed
 and/or modified under the terms of the Perl Artistic License