Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a C++ class from Perl?

Tags:

c++

perl

xs

I have a set of classes written in C++. What would be best way to call them from a Perl script? Thanks.

like image 302
Sam Lee Avatar asked Jun 01 '09 07:06

Sam Lee


People also ask

Does Perl have classes?

A class within Perl is a package that contains the corresponding methods required to create and manipulate objects. A method within Perl is a subroutine, defined with the package.

Is Perl c++?

Perl is an interpreted programming language. C++ is a general-purpose object-oriented programming (OOP) language. Perl can use closures with unreachable private data as objects. C/C++ doesn't support closures where closures can be considered as function that can be stored as a variable.


2 Answers

I'm not particularly fond of SWIG and prefer to write the interfacing code myself. Perl comes with a sort of pseudo language called 'XS' for interfacing to C or C++. Unfortunately, in order to use it, you will need to know at least C, Perl, and then learn something about the interpreter API, too. If you already know Perl and C well, it's not such a big step. Have a look at the following core documents on XS:

  1. perlxstut (XS tutorial)
  2. perlxs (XS reference)
  3. perlapi (Interpreter API)

Additionally, there's plenty of tutorials and how-tos on the internet.

Now, interfacing to C++ using XS requires some additional steps. It can be a bit frustrating to work out at first, but neatly falls into place once you get it. In this regard, the core documentation is sparse at best. But all is not lost. Mattia Barbon, the creator of the wxWidgets bindings for Perl, wrote a great tool "XS++" that makes this almost dead simple (or as simple as XS). It's included in Wx, but we're working on splitting it out into its own distribution. This is work in progress. You can find Mattia's XS++ code and a modified version of mine on github.

Barring a release of a standalone XS++ to CPAN, I would suggest learning to write XS for C++ from other resources:

  • Quite a long time ago, John Keiser wrote an excellent tutorial on XS and C++. It also includes further pointers to useful tools and documentation.
  • I learned XS&C++ from that tutorial and some examples I found on CPAN. I don't recall what I looked at then. But now I can point to my own work as a (good or bad, I don't know) example: Math::SymbolicX::FastEvaluator.
  • Similarly, the planned XS++ distribution contains a complete (albeit pointless) example of using XS++ to interface C++ and Perl. Since XS++ is translated to plain XS, you can use it to generate examples.

PS: There's also the Inline::CPP module. If that works, it is probably the easiest solution. I doubt it can handle templates, though.

like image 119
tsee Avatar answered Oct 05 '22 02:10

tsee


Check http://www.swig.org :

"SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby."

like image 30
Igor Krivokon Avatar answered Oct 05 '22 02:10

Igor Krivokon