Effective Perl Programming, 2nd Edition |
Author: Joseph N. Hall, Joshua A. McAdams, brian d foy This book lives up to its subtitle "Ways to write better, more idiomatic Perl" and is chosen as one of 2010's Best Books. Author: Joseph N. Hall, Joshua A. McAdams & Although I got to know the first edition many years after its publication, I quickly became a dedicated fan and discovered that it was still relevant despite Perl being constantly refreshed. It was one of those books that contained tips that get stuck in the mind and made you ponder 'Can this happen?', I did not know that...'; I still vividly remember getting acquainted for the first time with the Swartzian transform, caching a regular expression object or evaluating expressions inside strings.
This is also the case with the 2nd edition. Not only does it include tips from the first edition, which proves that the techniques displayed there are timeless, but it also enriches the material with new features introduced with each new version of Perl Perl is a high-level, rich and context-sensitive language that lets you express coding in many flexible and agile ways, (as they say, There Is More Than One Way To Do It), by taking advantage of its relaxed rules and un-restrictive nature. A very demonstrative example of Perl's natural ways and expressionism is found at page 25 : "next unless defined $cat". It surely resembles a construct of the English language. Can it get more natural than that? It is those properties that made it stand apart from other programming languages and allowed it to gain the passionate support of one of the most devoted developer bases. On the other hand this very agility can easily lead to unexpected behavior, as well as to underuse of Perl's strengths solely because of not fully comprehending its very rich features Hence the mission of this book: to disambiguate and demystify the language constructs, idioms and features which in return will unleash Perl's full potential and at the same time avoid the aforementioned pitfalls. It is only after you cope with the idiosyncrasies of the language by making them your friend rather than your enemy, that you will enhance your productivity, efficiency and derive pure fun on the way Saying that, this book will not teach you how to program in Perl at a fundamental level; it will not explain what a scalar variable, hash or operator is or go through explaining the flow and control structures. It assumes that you are already experienced with Perl. On the other hand it will enhance your understanding of the core language parts and show how they are used in a correct and efficient way or when to use one over the other ( i.e. when you use "&&" or "and"?). When appropriate, the use of related to the subject modules is demonstrated since you don't need to reinvent the wheel. It will also not show you how to traverse directories, fork a server, do IPC, networking,CGI or OO programming in Perl. As a matter of fact you won't find a code example more than 10 lines long. It does not include full blown programs but small snippets enough to get the point across. If that is not what you are after then you have to look elsewhere and fortunately Perl has a rich bibliography that covers anything needed. In fact it is mentioned in the books' prologue that you have to rely on other documentation for getting into depth. The book is targeted at the intermediate Perl programmer who wants to take coding to a higher level. If I had to, I would roughly break the contents down into the following categories. Keep in mind that this is not a strict categorizing as most chapters overlap in meaning: 1.Gotchas or how not to get unpleasant surprises because you did something and did not work as you thought it would. This is what Chapter 1 and 2 help to avoid by going through and explaining how to correctly and efficiently use the core constructs and idioms. For example, Item 6 "Know the difference between string and numeric comparisons" shows examples that can go wrong when using the == and eq operators and then goes on to explain the new smart match operator and how it alleviates those issues Item 7 would be handy to programmers particular coming from other languages where a false value can be just zero. This is not the case here as Perl has 0,'0',undef and '' evaluating to false. It is a good example of what could go wrong when checking for truthness by not using the defined operator; you could check for truthness without it, which of course is perfectly legal code and everything would work fine or seem to work fine because the condition that would break it did not arise. This also server as a demonstration of the unexpected behaviour principle caused by not having a good comprehension of the underlying concepts Item 10 shows how to correctly empty an array or to be careful with array slices and Item 18 goes through the very important subject of operator precedence gotchas. The rest of the chapter along along those lines but of course it couldn't leave out the notion of contexts, heavily used in Perl, since as the Camel book states "you will be miserable until you learn the difference between scalar and list context". 2.Hidden features, idioms, shortcuts, optimizations and less typing. You might be using the core elements of the language in a daily basis and do what you want to achieve without having discovered their hidden capabilities. For example you might be using the array but did you know how to re-order arrays or swap elements using slices as Item21,22 demonstrate or how to quickly create hashes? this could save a lot of typing. Or you might be using the sort function in the 'mundane' ways or the 'cool' ways. Both are legitimate, prove the TMTOWTDI principle once more and show that the language does not get into the way. You could also use foreach for iterating through an array and manipulate its elements but you could also use map and grep to do the same (or more) with a twist. Perl has extra fire power ready and available whenever asked for, In short it does not only show cool ways to do the same, but do it in a more effective, optimized and smarter way. 3.Best practices Chapters 4 and 5 go through best practices when using subroutines and filehandles. Item 47 use hashes to pass named parameters to subroutines and Item 49 shows how to use the subroutines to create closures, Tips include when to use local over my, avoid using @_ directly unless you have to and passing values to the subroutines by reference Item 52 "Always use the three-argument open" is another yet example of the TMTOWTDI principle. I can open a file using the two argument form or the three argument form. Certainly the first way is not wrong and Perl will not restrain you, but later may lead to problems and unexpected results, hence it is shown why one way is superior to the other. There are pros and cons in either approach and when you do something you must know why you did it and what to expect. Other examples of best practices are included in Chapter 11 which shows how to use warnings effectively to catch errors early and whenever you can't it shows you how to use exception handling and debugging features. Use Perl::Tidy, Perl::Critic to improve your formatting/style and coding, testing your module (Chapter 10) and using the database properly (Chapter 12) like how to use placeholders for quoting to avoid sql injections. 4.Concentrated and well detailed information on essential subjects acting as a reference guide Chapter 3 goes through the subject of regular expressions - one of Perl's strongest points is their integration into the language itself. It leaves some stuff out like lookarounds or how to execute embedded code in a regex, but it does a good job of going through the concepts, considering that the subject would easily fill a book Chapter 8 does not go through the Unicode theory but shows how to process it using Perl. The Encoding module, code points,normalization are subjects explored. When you couple Perl features and functions to work against Unicode text with Unicode enabled regexes features like properties \p{PROPERTY}, you will soon understand why Perl is the best choice for text processing. Chapter 6 tackles references and how to use them to create complex data structures and at the same time how avoid potential gotchas like circular data structures 5.Step by step directions How to use CPAN, how to build your module and distribute it 6.Decipering code When you look at a piece of code and scratching your head trying to figure out what it does, like Item17 my @a=@{ +func_returning_aryref }; or my @missing_h= grep { s/\.c$/\.h/ and The last chapter explores some pretty advanced subjects such as using pack and unpack functions which can be used for creating structures to talk with the outer world, Endianness, inside information on how the compilation process works and how you can affect it by using the BEGIN-end BLOCK. There are also some tips which I could characterize "tips beyond imagination" like the Swartizan transform explored in Chapter 2. In addition to the solid content and the authors' intentions to make difficult to grasp contents easier to understand, the formatting of the book is pleasing and inviting to eye and also utilizes colour for highlighting. All in all, there is a lot to be gained from reading it and it's one of those books that won't be accumulating dust in a bookshelf but will be used in a daily basis. It is a showcase of Perl's abilities and could easily become Perl's ambassador when you want to demonstrate what Perl is capable of. Also do not miss the great website the authors set up to exchange tips and get the conversation to the web. a great resource: http://www.effectiveperlprogramming.com/
|
|||
Last Updated ( Wednesday, 29 December 2010 ) |