|Intermediate Perl, 2nd Edition|
Page 2 of 2
.NET Linq to Objects anyone?
Using grep, filtering expressions are easy to assemble. This, of course, could also be done in procedural fashion with nested for or foreach loops, but this is Perl, and there is more than one way to do it.
But this is not the end of the story. Let's also apply transformations. With a map within a map we turn the input hash into a series of references to arrays. Each array will have a person’s name and one of the items they brought:
In Chapter 7, codrefs or references to subroutines are examined. Again it is all there, while the explanation on callbacks, closures, returning a subroutine from a subroutine and maintaining state are all nicely pieced together in a coherent way.
Chapter 8 is on references to file handles, contrasting the many of ways of managing them:
Since references are first class citizens we can even have Collections of Filehandles, storing them as array elements or hash values We then check then OO module alternatives, which allow writing our operations as methods:
and finally a look into Directory filehandles.
In Practical Reference Tricks, it is the turn of the sort operator to offer another intuitive way of exploiting data structures. Of course the discussion would not be complete without talking about the Schwartzian Transform, which has become something of a legend and the de facto firearm when engaging in "using a single example show my what your language is capable of " kind of language shootouts. Some reads and re-reads may be needed to get the hang of it.
Chapters 11 and 12 provide more about structurally organizing code into reusable units, explaining both why and how. It starts off by breaking the code into files and the various alternatives of loading those files/parts at runtime, mainly eval vs do vs require. But all of these options are not immune to namespace collision so the natural progression is to talk about Packages and getting deeper into it with Packages as Namespace Separators, Scope of a Package Directive and finally modules. We create a module Animal.pm, by utilizing boilerplate code and templates. This skeleton module will be used as our basic paradigm and be gradually enriched as we add code to it in the chapters that follow.
Then you reach the point where all your hard efforts so far pay dividends because you will be comfortable and ready to tackle Object Oriented Programming.
Chapters in the last third of the book explore Perl's standard OO system. Chapter 13 introduce class methods and inheritance, leaving more advanced features like instance methods and the distinction between them and the class ones, encapsulation, setters and getters, the inheritance hierarchies and UNIVERSAL until Chapters 15 and 16. The author's technique of breaking the material down into distinct parts makes it much easier to absorb.
Chapter 13 concludes by adding those OOP features to our module. What is also important is that Testing (Chapter 14) is introduced early on in the project's/module's lifecycle and revisited in Advanced Testing in Chapter 20.
So far everything tackled belongs to the default and minimalistic Perl OO realm. However, at Chapter 20 there's an excursion into the post modern one, aka Moose. Although it only touches the surface, we re-write our module using it Moose to give Perl an OO interface similar to the rest of classic OOP languages, with features like read-write attributes, explicitly stating inheritance, using the extends keyword, etc. But since it is Perl there is always going to be a twist, for example the addition of Roles favoring composition over inheritance.
Chapter 21 then competes the book by finally uploading and contributing our module to CPAN.
Summing up, the book it is not only a smooth and thorough guide to learning the language per se, but also shows how to use it in a complete development cycle, yielding a concrete outcome.
What I find astonishing is the cohesion and logical linking between the chapters, where knowledge is gradually acquired and correlated with the increase in the difficulty of the material. That, combined with explaining the reasoning behind everything, why you need to do it rather than just doing it, makes Intermediate Perl a book that isn't just about the language, but is an example of a teaching methodology at its finest.
|Last Updated ( Thursday, 06 September 2012 )|