Using Perl with compiled C library? - c++

I would like to try and use Perl, but need to communicate with another application I wrote. I have an interface library that I wrote in C++, and it contains the socket communications and interface protocol to set/get parameters in my application.
I would like to use Perl with Apache to serve up web pages for configuring my application. I need to know how to get Perl to talk to my interface library.

Oh Lord, if you are new to Perl. you do not want to be looking at XS (I am not new to Perl and I do not want to look at XS). See Inline::C or Inline::CPP for a much gentler introduction to calling C and C++ from Perl. Drilling deeper into the XS interface should only be necessary if you want to start passing or returning complicated data structures around (and maybe not even then).
use Inline C => Config => LIBS => '-L/<yourlibpath> -l<yourlib>';
$x = my_library_function_that_returns_an_int_or_double($integer_arg,$string_arg);

It looks like you want to communicate with a C library from Perl. You'll want to read through perldoc perlxs and perldoc perlxstut for more about the XS interface. If you want to do the reverse (call Perl from C), also read through perldoc perlguts and perldoc perlembed.
When you encounter specific problems, give SO another holler and someone can surely help you out. :)

If you chose not to go the inline-C route, Perl has built in support for sockets, A great tutorial on doing IPC, and a few objects to help with sockets in general (plus, google will provide you many other tutorials on using perl sockets). If you're able/willing to re-implement the sockets communication part, you may not even need the C/C++.

You need to create a module for you C++ library, if you don't want to use Inline::CPP. It can be done automatically with h2xs: h2xs --autogen-xsubs. Install C::Scan from CPAN first.

First, let me say that XS a relatively small set of keywords that makes wrapping C libraries for use from perl easier (see perlxstut and perlxs). In order to use it, however, you will have to learn the perl API (see perlapi) which in turn requires a shallow knowledge of how the perl compiler works (see perlguts and the somewhat outdated but still highly useful perl guts illustrated). That's a lot to learn at the same time. In short:
XS itself => not hard
perlapi => moderately hard
perl inner workings => pretty hard
all of the above at the same time => ouch
That being said, if you want to wrap a C++ library, have a look at the ExtUtils::XSpp module on CPAN. It comes with a very simple example. The module's not that old and still being improved, but it makes wrapping C++ a lot more convenient.
PS: If you plan to learn everything at once, learn by examples. Check out Dean Roehrich's cookbooks. Maybe use some simple XS modules as models: Parse::ExuberantCTags, Math::FFTW or one of the many others on CPAN.

Related

Does a C++ shell framework exist?

I used to develop some Perl programs using Fry::Shell. I think it is very powerful and easy to use.
For one of my C++ projects I need to create a command line client. The idea is to create a TUI like the one found in routing hardware.
Does such a framework exist ?
You can keep using Fry::Shell. It's not much of a hassle to call Perl from C++. Here's a starting point for that, there might be a better way to do it.
EDIT: I just found a project on Github. It's written in C and seems pretty much dead, but try it out, it might be useful. Even if it's not, since it's open source, you can use it as a starting point. It claims to provide a Cisco-like interface, which should suit you pretty well.

Lisp Interpreter in a C++ Program

I'm not sure I'm phrasing this right, but I'm pretty sure I'm looking for a LISP interpreter I can put in my C++ program.
The ideal situation I'm imagining is a function or something to which I can pass either a string, file, or filename containing the LISP code and then use the output from the LISP code in other parts.
To put it in terms of (print (eval (read))), I want read to be something I input to be what's read, and a string or something I can parse from print.
There is also ECL ("Embeddable Common Lisp"). It has the advantage, that it provides the full Common Lisp standard. Unfortunately, the documentation with respect to embedding is... well... a little bit scarce.
I never used it myself, so I cannot really tell, whether this would actually be an easy thing to embed into your application. IMHO, the Guile interpreter would be a reasonable choice.
Another embeddable Lisp is Rep, which is, for example, the extension language used by the Sawfish window manager. It started as Emacs lisp clone, but became something different over time, and is nowadays closer to Scheme.
If you're interested in Common Lisp, there's ECL.
From the FAQ:
2.1 What does this "embedding" stuff mean?
ECL is a full fledge
implementation of the Common-Lisp
language. However, due to the way it
is implemented, the implementation can
be used as an extensibility language
for your own application, much like
Guile works for the Scheme language.
By a rather simple set of functions,
you can parse, compile and execute
Common-Lisp forms, and using the
Foreign Function Interface (FFI), you
can add new functions to Common-Lisp
which suit your Domain-Specific
Language.
And:
2.2 How do I embed ECL into my application?
You have to use the ECL library, which
is called libecl.so, libecl.dyld or
ecl.dll, depending on your operating
system (Unix, Mac OSX or Windows). The
program ecl-config will provide you
with the flags you have to pass to the
C/C++ compiler and to the linker,
using either ecl-config --cflags or
ecl-config --ldflags, respectively.
Regarding your program, apart from
linking against the ECL library, you
have to call the proper initialization
routine, cl_boot(), so that ECL sets
up the appropiate internal structures.
After calling this routine, you will
be able to run lisp code and create or
manipulate lisp data
.
It's not Lisp, but Guile is the GNU cross platform scripting language, and is an interpreter/compiler for Scheme, which is pretty close to Lisp.
You can use GNU Guile, which is a Scheme interpreter specifically designed to be easily embeddable in C/C++ programs.
Try anything that comes up under this search: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=embedded+lisp
It turns out that a lot of people have wanted to do this.
For something even more embeddable than Guile, there's also Chibi.
(I <3 Guile BTW, so this answer isn't trying to take away from that---it just offers another option.)

Python, Perl And C/C++ With GUI

I'm now thinking, is it possible to integrate Python, Perl and C/C++ and also doing a GUI application with this very nice mix of languages?
Well, there is Wx, Inline::Python and Inline::C, but the question is why?
Anything is "possible", but whether it is necessary or beneficial is debatable and highly depends on your requirements. Don't mix if you don't need to. Use the language that best fits the domain or target requirements.
I can't think of a scenario where one needs to mix Python and Perl as their domain is largely the same.
Using C/C++ can be beneficial in cases where you need hardcore system integration or specialized machine dependent services. Or when you need to extend Python or Perl itself (both are written in C/C++).
EDIT: if you want to do a GUI application, it is probably easier to choose a language that fits the OS you want your GUI to run in. I.e. something like (but not limited to) C# for Windows, Objective-C for iPhone or Mac, Qt + C++ for Linux etc.
There's always Parrot. Here's the Wikipedia page. It's a vm to allow you access your favorite libraries from different languages in one application.
Everything is possible - but why add two and a half more levels of complexity?
Python & Perl? together?
I can only think of an editor.

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

I have a set of classes written in C++. What would be best way to call them from a Perl script? Thanks.
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:
perlxstut (XS tutorial)
perlxs (XS reference)
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.
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."
I would normally choose XS, like tsee, but there is also Inline::C (or Inline::CPP in this case). I dislike SWiG and tend to avoid packages built around it.

How to embed Ruby in C++?

What's the best way to embed Ruby as a scripting language in C++? Using ruby.h? SWIG? Something else? What I need is to expose some C++ objects to Ruby and have the Ruby interpreter evaluate scripts that access these objects. I don't care about extending Ruby or accessing it in C++.
I've found this article on embedding Ruby in C++, and while it's very useful, it's kinda old and I was wondering if there are any other resources on the subject (or open source implementations of similar engines).
Rice is looking very promising.
Ruby provides a very helpful README.EXT file. It has lots of information about how to extend Ruby, and convert between C & Ruby types.
There is also this excerpt from the pick axe book which pretty much covers the same thing.
In my case, when I added Ruby scripting to my application I decided against using swig, because my needs were very simple, and I didn't want to add yet another build dependency.
swig is probablly the way to go..... but ruby doesnt embed too well......
if you want a language that embeds nicely into C++, try lua
You might wish to check out tinyrb.
I've been working on Rarity (https://github.com/Plaristote/Rarity), which does two things:
Generates Ruby bindings from a YML description of your C++ API
Allows interaction with Ruby script in the most C++ fashion there is
I've solved a good deal of question (exception handling, garbage collection)...
I haven't seen anywhere else the code generation that Rarity uses to make your bindings come to life. I also haven't seen any other lib that allows such an easy conversion between C++ and Ruby types.
I think Rarity's worth the shot ! And I'd be glad to have some feedback as well :) !