Clear questions and runnable code
get the best and fastest answer
Getting Started with Inline::Cby derby (Abbot)
|on Mar 12, 2002 at 13:52 UTC||Need Help??|
I like Inline and I especially like Inline::C. There is great power for good and evil with Inline so below is my quick guide to using and hopefully not abusing it.
What is InlineInline allows you to simply embed other languages inside of perl. Right now there are about ten languages you can Inline - Assembly, Awk, Basic, C++, Guile, Java, Octave, Python, Ruby, Tcl and of course C. There are other things you can do with Inline (like reverse it and embed perl in C) but I'm going to stick with the more common form.
WhyWhy would you want to use Inline? That's a good question and I can think of three answers:
Why NotOf course there are reasons you shouldn't be using Inline:
CompetitionInline is the new kid on the block. There are other mechanism for embedding other languages into your code. There is the venerable perlxs as well as swig. I cannot comment too much on swig but I can say that Inline is much easier to start with than XS.
Where to startYou should do a lot of reading before you decide to Inline. Here's a good list:
Tools Needed for Inline::CTo continue with the rest of this tutorial, you should ensure you have a c compiler and the Inline module (which includes the Inline::C module).
The cannonicalThe following code is just a simple straight forward snippet of Inline that prints everyones favorite line:
Exposing a LibraryThere is a nice little library that exposes the services file and lets you view, add, delete of modify entries in it.
In order to use the rest of the examples, download and install the services library. Make sure you install it in a directory where your compiler/loader can find the header and library. You can still use Inline if the files are in an odd location but you would need to configure your Inline properly.
This next snippet of Inline just exposes that part of the services library that reads the file. (This is also a sub-benefit of Inline, you can pare-down or wrapper only those parts of a library which you deem important).
If you run this code, you will see a listing of all the services contained in your services file. But this isn't really useful. Let's slightly modify this example to add the services to the stack, thus returning an array of of services that we can work on in perl world:
Now that's pretty powerful.
SpeedNow we could parse the /etc/services ourselves and just have a pure perl approach. The next snippet of Inline shows that and bench marks the two approaches:
And the results of benchmarking are:
So we can see the Inline version is much faster than the perl version (but I'm sure there are other pure perl implementations which would be faster than what I've done here).
Thanks and CreditsInline would not have been possible without the hardwork and dedication of Brian Ingerson. Also, check out the official Inline site.
updated: return type for hello_inline function was incorrect (thanks ariels). Also just recently read tye remarks about posting snippets to pm should cannocalize the shebang to /usr/bin/perl (I just wish I could find that node again - no luck with search or supersearch).