use Perl Log In
Book Review: Extending and Embedding Perl
Tim Jenness, Simon Cozens
Review By Brandon Willis
Conway Perl Mongers
Enjoy using Perl? Ever marvel at how Perl can do the right thing but still be written in C? Extending and Embedding Perl aims to take the black magic out of understanding our favorite language. In fact, the authors flat out admit they think it is unfair that only über-hackers get to have one foot in Perl and one in C. Tim Jenness and Simon Cozens attempt to break down that barrier with lots of annotated code examples, direct analogies from the structures in Perl to those in C, a fine-grain look at XS and what it takes to robustly use a Perl interpreter in C.
What's that up your sleeve?
It is my experience that many situations require us to thoroughly examine a solution to understand how to best use it effectively. Perl is no exception. The ability to bring such a force as Perl to a project at the proper time is a valuable skill to possess. However, wading chest deep into XS and the Perl internals is not for the faint of heart. Jenness and Cozens ease this process by stepping in lightly at first.
What's in it?
The book begins with simple C examples that are then related back to the
readers' knowledge of Perl. Then the text seems to throw us a curve by leaping
off into building Perl modules. But there is method to the madness. Building
Perl modules correctly is inextricably linked to XS. Light introductions to XS
are performed and the reader is well on his/her way to building
After building a very specific foundation of simple C examples, module building, and some XS, the text returns to C to introduce pointers, arrays, file I/O and memory management. With these new skills we begin to explore the structure and implementation of Perl variable types. Chapter 4 provides many useful diagrams of how Perl variables appear and into which C structures they translate.
Still following a logical and constant order, we explore the Perl 5 API, learning how to post and retrieve information to the variable types explored in the previous chapter. As much as it might seem, this is not a rehash of the perlapi doc. It is consistent with the perlapi doc, but Jenness and Cozens provide extensively annotated C code examples.
Casting deeper still, we add the advanced C of pointers, arrays, file I/O and memory management to our knowledge of XS. At this point we have everything we need to effectively extend Perl, but the text continues deeper still by exploring how XSUB interfaces to Perl's internals. It is only the clearly documented, step by step explanations of this chapter that make it manageable for an average user like myself. Chapter 7 ends our stint with XS by discussing some alternative XS (or equivalent code) generation suites.
Switching gears entirely, we grab libperl.a and stuff into a C program. Chapter 8 begins the task of embedding Perl into a C program. Jenness and Cozens continue the embedded discussion through a Case Study in Chapter 9 and end with a look through the Perl internals in Chapter 10.
The final chapter (Chapter 11) details some of Perl's history, its development process, how we could become involved and what the future of Perl and Perl 6 may entail.
This book was indispensable in gaining a good foot-hold on using Perl in, from, and around C. I found it a good reference guide as well as an easy linear read. It is not a replacement for the perlguts, perlapi and perlxs documentation. But then again, it doesn't try to be. The annotated code examples with every line explained make following the book with development of your own solution a lot easier than in some other books. However, the in-depth explanations can be a bit frustrating for the impatient."