Mastering Perl is a book from brian d foy
If you like this post, read my other Perl books reviews π
Becoming a Perl master
This is a good paragraph title because this is what this book is about, to make you a better programmer and particularly a better Perl programmer.
The goal is even more fairly high since it aims to (give you hints) to make you a master.
There is an interesting introduction about how to become a master in general, how it involves studying methods from multiple masters, working on a masterpiece and knowing multiple languages.
Book structure
The book is composed of a serie of totally independent chapters. Each chapter focuses on one topic and has the same structure: a kind of introduction, the main content with paragraphs, a list of resources and a conclusion.
There are a lot of names dropping and references so you can continue your path to mastering π
Reading this book requires to know Perl basics.
Regex
Regex as first chapter is a bit rough π but there are a lot of interesting bits like explanations about modifiers, lookahead
and lookbehind
, the use re "debug";
Security
This chapter suffers from being old (the book I have into my hands was published in 2007) and all topics are very well known today (but does not mean they are no longer true).
I enjoyed the description of different forms of system calls and the explanation about setuid.
Debugging Perl
A lot of very nice tricks here, like redefine
or wrapping subs or even using -d
for a Devel::
shortcut π
Have you ever used all debugging functions from this list?
-
print
(of course! Who does not?!) -
warn
(yes, a customized print) -
croak
(people says it is the magic wand) -
carp
(how is it different from croak?) -
cluck
(WUT?) -
confess
(how many are there?)
Profiling
This is a great and not so great chapter in the same time. There is plenty of profilers listed here like Devel::SmallProf (not updated since 2007, still good?) or DBI::ProfileDumper or Devel::Dprof (deprecated).
I must add Devel::NYTProf because it's a must! I think it is not mentioned because it was not created when this book was released.
There is even a paragraph to explain how to write your own debugger.
It's not so great because the first edition of Mastering Perl is old, code is not really modern and it is a bit "too personal" for me (this last point could be a good point for other people, matter of taste).
Benchmark
In addition to pure benchmark tips like -DDEBUGGING_MSTATS
compilation flag and benchmark functions like timethese
there are technical details from perl internals (and this is something I really enjoy)
"perl trades memory for processing speed" or "perl does a lot of lookup instead a lot of compilation" or even about memory management (perl takes big memory chunk and tries to reuse them)
Some usage of Devel::Peek and Devel::Size are showing how perl behaves (SV, PV, PVIV, how data is stored) and how we can inspect this even from running program.
I really loved this chapter!
Clean Up Perl
This chapter is about managing a codebase and does not fall into the trap of giving details about coding style but keeps about generic concepts:
- "Code isn't bad because a novice Perl programmer can't read it"
- "[You should] Be able to understand handful major coding styles"
- "Be coherent and mimic existing style"
Typeglobs
Very interesting explanations about scopes (my
, local
, our
, packages
) passing functions as parameters or return values of functions.
It opens the door to the next chapter about "Dynamic Subs"
Dynamic subroutines
A lot of black magic here about references, symbolic references, Data::Constraint (that I found pretty clever), Autoload, Autosplit.
Code samples are dirty: hacks + goto
+ &{$AUTOLOAD}
+ regex + $1
+ $_
+ no stricts
+ $sub = sub
+ die
+ $_[0]
But hey it's gory details, at some point if we want to understand all Perl internals...
And as I said it is really black magic!
Modifying and jury rigging modules
This chapter explains how to debug/patch modules.
For the record:
- Local patch with very high version
- Redefine sub in caller code
- Wrap sub in caller code
- Subclass (if possible) sub in caller code
Configuring Perl programs
Various methods described:
- Config in Perl code
- Config in external Perl code (e.g. code.pl
require
config.pl) - Config in ENV vars
- Command line args (via GetOpt or
perl -s
). Raku has MAIN - Config file (ini, yaml...)
- Configure according to filename (modern trick like git-log)
- Interactive questions (if interactive shell
-t STDOUT
) - Heavy config
use Config
Error reporting and logging
We don't really have exceptions in Perl, we have eval
+ die
to simulate or try
/catch
coming into core right now but is it not universally used. The chapter details about sending and retrieving errors or propagating them.
Logging chapter presents some logging facilities and especially log4perl
which is very powerful.
Data persistence
Once again this chapter gives you pointers:
pack
- Data::Dumper to serialize as string
- YAML
- Storable
- DBM (not always portable if we use different driver)
Concerning serialization, I must add Sereal, ProtocolBuffer and Avro.
I think none of them existed when the book was released.
EDIT: some more... MessagePack, XDR π
Working with bits and tied variables
Diving into functions "close to material" with vec
or Bit::Vector an Tie::Cycle or Tie::BoundedInteger etc...
Modules as programs
Very nice trick with caller()
Tricks in bulk
-
@{[ ]}
see baby cart - using
keys map
to untaint -
WARN
is signal! -
L::*
(I think it's like Local::*) -
dualvar
for error for instance - END blocks are executed after a
die
-
$^E
for OS specific errors - You can put perl code in log4perl conf
- Data::Dumper does not work for subs
-
vec
can be a lvalue -
make disttest
,make tardist
Citations
- "Humility is the principal virtue of a maintenance programmer"
- "Talk to your Perl therapist"
Conclusion
While being imperfect (mainly due to the age of the book, only one and last edition in 2007), Mastering Perl is rewarding and teaches a lot of useful things from programming concepts to Perl details (more the latter actually).
I can recommend this book since I learned a lot of tricks π
Top comments (3)
Thanks for the review. I wanted to point out that there is a Mastering Perl, Second Edition which is from 2014: oreilly.com/library/view/mastering...
I highly recommend using that.
BTW for the last ten years, if I find outdated perl books somewhere, such as old versions of the Camel, I throw them away, and I would recommend other people to do the same. I do not want people opening the book and getting the wrong impression about the language. Perl in 2021 (or even in 2014) is much different than turn-of-the-century perl.
Bryan has a new book coming out: amazon.com/Learning-Perl-Making-Th...
sounds like very good book :)