Author: Randal L. Schwartz, brian d foy & Tom Phoenix
Aimed at: Beginners wanting to learn Perl
Reviewed by: Nikos Vaggalis
This book, nicknamed "The Llama", is almost legendary. This review reveals a lot about Perl as well as the book.
Learning Perl is the creation that sprung out of the co-operation of three of the most prominent names of the Perl world; Randal L. Schwartz, Tom Phoenix and brian d foy. They have a lot of titles under their belts, see for example our reviews of Effective Perl Programming, Intermediate Perl and Programming Perl, aka the Camel book. So they surely know how to address both a beginning as well as a more advanced audience.
Keep in mind that a beginners' book is more difficult to write than a specialized one since a vast array of experience or in-experience has to be covered, but again, the authors are up to the job. This title has auspiciously reached its 6th edition and has been fully updated up to Perl 5.14; since a lot has changed from version 5.10 to 5.14 this revision was needed to bring everything up to date.
The book keeps the familiar structure and content of earlier editions but updates them with the new features (such as the state and say keywords, the given-when control structure etc), while focusing strongly on Unicode.
Chapter 1 goes through the structure of the book, what Perl can be used for, a little bit of history, how to get Perl and install it and shows the first few lines of code.
Chapter 2: Scalars covers one of the basic Perl data types. A scalar can hold numbers and strings interchangeably and Perl knows when to use each. The chapter shows how data is stored and represented in Perl, how Perl handles the conversion between strings and numbers, how to assign to a scalar, comparison operators and some other basic but important to know stuff, like the undef value and checking for truthiness with defined.
Chapter 3:Arrays looks at the second basic data type. Arrays are used for storing collections of items; their difference with Lists is examined and is shown how to store, extract and manipulate data with the array. Of course it cannot skip the very important subject of the scalar and list contexts which can easily make the life of a Perl programmer miserable if not thoroughly comprehended.
Having already met and used several core functions, it is time to create our own User Defined ones. Chapter 4 shows how to define and invoke subroutines, potential gotchas with the local 'my' variables, passing parameters into a subroutine (Perl has optional and variable length parameters) and getting return values back. Best practices are covered, for example the use of the strict pragma and calling a subroutine with or without prepending the ampersand to its name. Here we meet the state keyword, new in version 5.10, which acts just like the static keyword in C so you don’t have to use closures to keep method state.
Chapter 5 covers the very important subject of Input and Output and the many and flexible ways Perl goes about it using STDIN, STDOUT, the Diamond operator, @ARGV and file handles.
Chapter 6 looks at the last fundamental data type, Hashes, which store key-value pair collections. It shows the advantages of their use as well as how to manipulate data with them
Then, Chapters 7-9 are solely devoted to Regular Expressions.Yes, three full chapters devoted to the subject in which Perl has, does and will excel over other languages.
The Perl regex engine has become an industry standard as regexes are classified as Perl compatible ones and the rest. It is an area in which Perl drives evolution.
Perl makes working with Unicode and text processing in general, a breeze; not just because of the sheer power of its regex engine but because of its tight integration into the language itself. Compare the use of regular expressions in another language like C# or Java where numerous classes have to be summoned and everything wrapped into objects before you can actually apply the expression to the target text, to Perl's simplicity and efficiency
These chapters give an introduction to what regular expressions actually are and examines their building blocks : metacharacters, quantifiers,character classes and much more. In the later chapters these basic constructs are put together in simple ways to matching text. Later on, they are spiced up with more twisty and advanced constructs such as using Named captures, introduced in version 5.10, and automatic match variables. Also regex operator precedence is explored, which can shed light into why your carefully crafted regex does not function as planned.
After matching text we move on to matching and substituting text. /g and friends come along, as well as non-destructive substitutions, a feature that is not earth-shattering but makes life a bit more convenient.
After mastering =~ do you think it is all over? No, the fact is that build-in Perl operators like split and join can also leverage the power of regexes as well! Many examples follow to exhibit their usage.
Chapter 10 covers control structures. Perl has more control structures than normally met in a language and this is a sign of the TMTOWTDI (there's more than one way to do it) principle that characterize the language. Hence, as well as the classic if-then-elsif-else we also get unless, until, while, naked bocks, labeled blocks, the ternary operator and much more.
Chapter 11 covers extending the core language through modules. As far as extensibility goes, Perl holds one of the biggest weapons, a feature that all other programming communities envy: the CPAN collective; a repository of ready to consume libraries on almost anything imaginable....
Does the core language not provide the functionally you are after? Don't re-invent the wheel ; fire up the CPAN shell and install a relevant module!
(The CPAN shell is like the .NET BCL with the difference that you don’t have to download the whole .NET framework binary, but can just consume individual libraries.)
This chapter covers how to find, install,and import a module's functions into your program. It also gives an introduction to the most important and popular modules such as File::Spec for file processing, CGI for the web and DBI for accessing DBMS and manipulating databases with SQL
As a side note, talking about extensibility, Perl6 (well not the language itself but the Parrot VM) will allow you to call libraries of other dynamic languages, for example you can call Ruby libraries from within Perl and vice versa! This is how Perl loosens its tight coupling to C and enables you to use an actual VM
Chapters 12 and 13 are concerned with file testing (is that file a directory, is it readable, who owns it?) and directory processing. These are the most valuable chapters for sys admins. On the way it also provides an outlook on how the OS stores and handles access to the files as well as what other information is attached to them.
Just as you were thinking that regexes were enough for text processing, along comes Chapter 14 presenting more options for working with text. Functions like index and sprintf help in finding,manipulating substrings and formatting data while an introduction is given to the language's myriad ways of data sorting.
Because Perl is able to store both numbers and strings into the scalar data type there is always the lurking danger of getting the wrong outcome from doing comparisons, hence the smart match operator was invented to help with those kind of issues. It looks at both operands and makes the best decision on how to compare them.
Then, a feature long overdue and highly sought after was a C-like switch statement, so you do not have a dozen if-then-elsif statements one after the other, and comes in the form of the the given-when. However it is a like a switch but with a twist!
Chapter 16: Process Management provides a very good overall and holistic view on Perl interacting with the rest of the world, the OS that is. It covers how to spawn external processes and consume their output within your Perl program, with warnings and gotchas where the OS can intervene and mess up your calling external processes,interacting using signals, as well as covering forking which allows for running processes in parallel.
Chapter 17 ends the journey with some advanced techniques such as using slices to quickly extract the data you need from an array or hash, which also serves as a display of the language's highly idiomatic behaviour. Exception handling through eval is covered (Perl has not STH exception handling built into its core, although you can get that by using a module) and neat tricks are demonstrated that not only make your code more optimal and efficient but offer a showcase of what you can pull off with Perl when you later on master the language and tame its power. An example of that is using grep to filter data from within a list or using map to transform that data. You can also chain functions where one functions’ output is gathered by the next one, with the most famous example being the Schwartzian Transform.
Appendix A holds the answers to all exercises.
Appendix B goes through subjects not covered in the book since they belong to a more advanced level of experience but they are there for letting the reader get a glimpse on the vast amount of usage scenarios that Perl can be applied to (math, sockets, OOP, threading etc)
Appendix C acts as Unicode primer introducing it to the uninitiated
The book is well written, carefully planned with each chapter logically anchored to the previous one, and follows a tutorial-based approach which renders it highly usable in a classroom.
Its main aim is to get you started with the basic building blocks of programming in Perl while backs theory with practice by requiring the reader to tackle the exercises at the end of each chapter
The chapters themselves are short and contained and look like they have just the proper material and length as to not tire and disorient the beginner while humorous comment, even self-sarcastic ones, scattered throughout the book help lighten the load on the beginners’ shoulders and encourage you to try again if at first you don't succeed.
If you are looking for an agile,modern,robust, truly multipurpose and multiplatform language as well as one that is dynamic (in attitude) and that will constantly surprise you no matter how many years of experience you possess or will possess, then this 6th edition of the legendary Llama book will provide you with a first class opportunity to start learning Perl.