I have a set of classes written in C++. What would be best way to call them from a Perl script? Thanks.
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.
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.
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:
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:
PS: There's also the Inline::CPP module. If that works, it is probably the easiest solution. I doubt it can handle templates, though.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With