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, 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



Deep Learning: A Practitioner's Approach

Author: Josh Patterson and Adam Gibson
Publisher: O'Reilly
Pages: 536
ISBN: 978-1491914250
Print: 1491914254
Kindle: B074D5YF1D
Audience: Java engineers and practicing data scientists
Rating: 3
Reviewer: Mike James

AI is important and profitable, is this a good introduction?

Risk-First Software Development: The Menagerie

Author: Rob Moffat
Publisher: CreateSpace
Pages: 248
ISBN: 978-1717491855
Print: 1717491855
Kindle: B07MK9LTHN
Audience: Those working on software projects at intermediate level
Rating: 4
Reviewer: Nikos Vaggalis

This first volume of the Risk-First series, looks at managing software project [ ... ]

More Reviews





Last Updated ( Thursday, 06 September 2012 )