C++ インターフェースの複雑さによっては、SWIG を省略して自分で XS コードを作成する方が、簡単で、高速で、保守しやすい場合があります。XS&C++ は少し難解な技術です。そのため、Mattia Barbon の優れたExtUtils::XSppモジュールが CPAN に用意されています。これにより、C++ のラッピングが簡単になります (そしてほとんど楽しくなります)。
ExtUtils::XSpp ディストリビューションには、文字列 (char*) と整数メンバーを持つクラスの非常に単純な (そして工夫された)例が含まれています。カットダウン インターフェイス ファイルは次のようになります。
// This will be used to generate the XS MODULE line
%module{Object::WithIntAndString};
// Associate a perl class with a C++ class
%name{Object::WithIntAndString} class IntAndString
{
// can be called in Perl as Object::WithIntAndString->new( ... );
IntAndString();
// Object::WithIntAndString->newIntAndString( ... );
// %name can be used to assign methods a different name in Perl
%name{newIntAndString} IntAndString( const char* str, int arg );
// standard DESTROY method
~IntAndString();
// Will be available from Perl given that the types appear in the typemap
int GetInt();
const char* GetString ();
// SetValue is polymorphic. We want separate methods in Perl
%name{SetString} void SetValue( const char* arg = NULL );
%name{SetInt} void SetValue( int arg );
};
これにはまだ有効な XS タイプマップが必要であることに注意してください。これは非常に単純なので、ここでは追加しませんが、上記のリンクのサンプル ディストリビューションで見つけることができます。