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

Author: Randal L. Schwartz, brian d foy, Tom Phoenix
Publisher: O'Reilly
Pages: 396
ISBN: 978-1449393090
Audience: Intermediate Perl programmers
Rating: 5
Reviewer: Nikos Vaggalis

You have gone beyond the basics with Perl and want to consolidate your knowledge and produce some useful programs. Is this the book to help you?

The scenario is that you've just completed Learning Perl and you are wondering what's next, how do I progress to the next level ?

This is where this book fits in. It is the second part of the three part series comprising of Learning Perl, Intermediate Perl and Mastering Perl, with each one targeting a specific programmer level. The whole series could very well form the syllabuses of a College course in programming Perl, with the current title being part of Syllabus II.

Intermediate Perl aims at leveraging what you've learned last semester and channel your efforts into building something concrete and useful while at the same time cultivating and enriching your skills with new found knowledge. However, having studying Learning Perl is not a prerequisite, if you are beyond the novice level; of course it  helps if you are familiar with it since you will also be familiar with the style adopted. It's a tutorial approach with coursework at the end of each chapter.

The good thing is that the material is written by people who know how to structure a course, having been doing it for ages and hence well aware of what's needed for progressing the reader from Beginner to Intermediate.

 

Banner

 

After Chapter's 1 smooth introduction into what's going to follow and what the book is all about, we get into Chapter 2 where, instead of jumping straight into code writing, modules are tackled. And there is a very good reason for that. The notion, from the beginning, is to get you into the mindset of simplifying your development effort (and saving much valuable time too), by not having to reinvent the wheel. There is a good chance that what you are after has already been implemented. Hence Chapter 2 goes through the modules included in the standard distribution, as well as how to summon the CPAN ones into your projects - finding the right module, evaluating it, reading its documentation, figuring its dependencies out, installing and finally using it by importing its functions. It also considers whether the module uses a functional or OO interface, watching out for function name collisions, while at the same time giving a rundown on some of the most basic but most useful modules of the standard distribution like File:Spec.

But the highlight of this chapter is the installation part; it really leaves nothing to be desired. There are complete instructions on how to set the modules' search path by means of @INC, use lib,Findbin, PERL5LIB, even instructions on how to install without having sys admin privileges by means of local::lib (especially handy for constrained web hosting environments) as well as using various client applications to get to CPAN (cpanm,cpanp,etc).

It then starts to build stone upon a stone, setting the foundations in the early chapters and preparing readers for the more advanced ones, hence the book should be read in a linear fashion; no jumping of chapters back and forth. So Chapter 3 looks into prerequisites; going into detail with the list operators grep and map, the block and string form of eval, plus a rundown on the much misunderstood and thus underused do operator. What is of most importance, is that the authors build a protective environment around the reader, guarding you from the potential pitfalls arising from using those operators since it is well known that Perl, although very versatile, also provides enough rope to hang yourself, which can be especially detrimental for inexperienced programmers.

Various gotchas are also laid out. For example while demonstrating grep's block form we are also warned that: "We would have been wrong to keep the return because we’re no longer in a separate subroutine: just a block of code. A return in the grep would have exited the subroutine that contains this entire section of code. And yes, some of us have been bitten by that mistake in real, live coding on the first draft." .

This approach, which fortunately persists throughout the book, helps in understanding why the code you wrote does not do what you think it would do.

Chapter 4 continues that way, talking about References and Complex data structures. Starting from the basics. like Taking a Reference to an Array,  it smoothly builds up the momentum for getting to complex data structures, such as an array containing references to other arrays. Since a picture is worth a thousand words, PeGS (Perl Graphical Structures) is used, which aids the reader into visualizing those structures, something that makes the underlying hierarchy and inter-relations much easier to comprehend.

It is also clearly explained why references are needed, the difference between byval and byref, and again warnings of potential pitfalls for example when dereferencing an array reference:

"However, we cannot drop the braces if the value within the braces is not a bareword identifier with one or more leading $s. For example, for @{$_[1]} from that last subroutine rewrite, we can’t remove the braces"

or when Simplifying Nested Element References with Arrows:

"More simply, using the “drop arrow” rule, we can use: $root −> [2][1][0] We cannot drop the first arrow, however, because that would mean an array @root’s third element, an entirely unrelated data structure"

There also follows a very helpful example of breaking the thinking process of complex dereferencing, for example @{$root−>[2][1]}), down into smaller parts, which aids in the better comprehension and in deciphering code written by a third party.

Then Chapter 5 shows that Perl treats references as first class citizens since "we can copy and pass around references like any other scalar" and moves from the named references to the concept of anonymous ones and autovivification (mainly used with hashes) while also introducing Perl's reference counting GC system and when it goes bad, aka circular references.

Chapter 6 starts off with debugging, serializing and storing those data structures. The concepts explored in the previous chapter set the scene for this chapter's examples of map and grep and display Perl's succinct and declarative style of programming. In the examples following, grep is used to interrogate objects,i.e an array which contains further array references:

my %provisions = (
'The Skipper' => [qw(blue_shirt hat
              jacket preserver sunscreen) ],
'The Professor' => [qw(sunscreen water_bottle
             slide_rule batteries radio) ],
'Gilligan' => [qw(red_shirt hat
              lucky_socks water_bottle) ],
);

for filtering and extracting the sought after information: looking for "Who brought a water bottle?"

my @all_wet = grep { my @items = @{
  $provisions{$_} };
grep $_ eq 'water_bottle', @items;
} keys %provisions

 

<ASIN:1449303587>

<ASIN:0596527241>

<ASIN:0321496949>



Last Updated ( Thursday, 06 September 2012 )