Note
We are no longer accepting new customers or work orders at this time. Thank you for your interest.
Description
As powerful as Perl is for writing sophisticated scripts and software applications, there are certain situations where its use is not suitable - for example, with highly CPU-intensive computations, such as encryption/decryption code, fractals, ray-tracing, talking directly to hardware devices, things like that.
You may choose to do the bulk of an application's coding in Perl and call out to a piece of inline C code to do just the special case portion of the work.
The Old Method: Launching a Process
Depending on your application, there are many ways to run some C code from Perl. Probably the oldest and easiest way is simply writing a C program and launching it from Perl with a "system" command, back-ticks, or "open-pipe" command. The C program can accept data via command line args, STDIN, or by reading a file. It can produce results by writing to STDOUT or to another file. And then Perl can read those results, and act accordingly.
But sometimes that simple model just won't work in your situation. This strategy requires a huge overhead of fork-and-exec, to completely create a new process, hand over control to it, let it do its job, and only when it finishes, can the Perl script take control again. That's slow - if you're calling it many times per second, it can substantially slow down your whole application.
Oh sure, there are ways of keeping both Perl script and C program running simultaneously. IPC methods can be used to monitor each other, possibly communicating through various mechanisms like named pipes, Semaphores or Shared Memory, if that's what you need.
Inline::C Module
Sometimes all you want to do is write a Perl subroutine in C. It should look and feel like a regular Perl subroutine call, with the core of the work being done by some compiled C code.
One way to accomplish this is with the Inline::C module. You can find it on the CPAN web site, among other places.
For more information visit the Inline C Cookbook.
Inline::CPP Module
This module is an Inline::C variant for C++.
Inline Module
To learn more about the Inline module concept, visit O'Reilly's article Pathologically Polluting Perl on the perl.com web site.
Perl XS
This is the underlying interface which Perl uses to call a C or C++ subroutine. XS stands for "eXternal Subroutine", meaning external to Perl itself (calling a subroutine written in a different language).
Wikipedia's article on XS (Perl) has quite a useful explanation.
You probably won't need to talk directly to XS, as the options described above are higher-level interfaces which can do it for you.
O'Reilly Books on the Subject
As you may already have guessed, there are some good O'Reilly books about this stuff.
The book Advanced Perl Programming, second edition by Simon Cozens
is a great addition to your library if you don't already own it.
Chapter 9 is all about inline extensions with Inline::C and more.
Conclusion
I hope this article has helped you understand more about Perl and inline C code. By optimizing critical CPU-bound sections of code in C and calling them from Perl, you can really get the best of both worlds.
Don't miss the latest perl tips and tricks!
Subscribe to our low-volume mailing list:
Privacy Policy
| Copyright © 2006 - 2010 Keith Smith Internet Marketing LLC, all rights reserved. |
| Problem with this web site? please let us know |