Intermediate Perl, 2nd Edition
Article Index
Intermediate Perl, 2nd Edition
Object-Oriented Perl

.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:

my @person_item_pairs = map {
my $person = $_;
my @items = @{ $provisions{$person} };
map [$person => $_], @items;
} keys %provisions;

 

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:

  • The Old way: using barewords for programmer-defined filehandle names
  • The Improved way: Instead of using a bareword for the filehandle name, we use a scalar variable whose value is undefined
  • Filehandles to Strings : Since v5.6, we can open a filehandle to a reference to a scalar instead of to a file

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:

  • IO::Handle and Friends
  • IO::File
  • IO::Scalar
  • IO::Tee

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.

Related Reviews

Learning Perl, 6th Edition

Programming Perl

Effective Perl Programming, 2nd Edition

 

Banner


SQL in Easy Steps, 3rd Ed

Author: Mike McGrath
Publisher: In Easy Steps, 2012
Pages: 192ISBN: 978-1840785432
Aimed at: Beginners in MySQL
Rating: 3
Pros: Step by step instructions on creating a SQL query
Cons: Lacks any view of the underlying ideas you need to understand
Reviewed by: Kay Ewbank



3D Game Programming for Kids

Author:Chris Strom
Publisher: Pragmatic Bookshelf
Pages: 250
ISBN: 978-1937785444
Audience: Beginners to programming looking for a games-led approach
Rating: 4.5
Reviewer: Mike James

JavaScript is a popular programming language and introducing it to kids is a great idea and what could be better than  [ ... ]


More Reviews

 

<ASIN:1449303587>

<ASIN:0596527241>

<ASIN:0321496949>



Last Updated ( Thursday, 06 September 2012 )
 
 

   
RSS feed of book reviews only
I Programmer Book Reviews
RSS feed of all content
I Programmer Book Reviews
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.