Calling Other Languages from Perl
During October's meeting, Jay asked how to call Python code from Perl. This brings up the larger question of how you might interface between code written in other languages from your Perl code.
There are three major approaches to solving this problem. If the code you need to communicate with is a standalone program, it is relatively easy. The methods you might want to try are briefly covered in the presentation.
If the code is in C, you can use the XS interface which is traditionally used to build Perl extensions. There is also a new interface called SWIG which is supposedly somewhat easier. Both of these interfaces require a investment in time and effort to set up and learn the interface tool. When trying to quickly find a way to call a library written in another language. This may not be the fastest route.
This leads us to the
Inline modules that were created by Brian
Ingerson. Ingerson created the
Inline::C module and support code
to make calling C code from Perl much easier. He also built the framework that
allows other languages to be called in the same way. Most of the
presentation is focused on these modules.
There are also a few gotcha's I found along the way that did not become part of the presentation. These were covered in the talk, so I'll add them here.
- When using
func( void )for a function with no arguments.
- When using
Inline::CPP, make certain the interface between Perl and your code contains no template classes or functions. The actual code can use templates, but the interface cannot.
- If you can't get the code to recompile, check the build directory under _Inline. If nothing else, delete the appropriate directory and try again.
On a final note, you can actually use the
without writing much new code. By
importing the appropriate headers/libraries/etc., you can expose
the interfaces you need to the
Inline modules. Those modules will
supply the wrapper code that you need to make the calls from Perl.