Saturday 29 January 2011

Higher-Order Perl: Transforming Programs with Programs



Higher-Order Perl: Transforming Programs with Programs
| 2005-03-14 00:00:00 | | 600 | Perl


Most Perl programmers were originally trained as C and Unix programmers, so the Perl programs that they write bear a strong resemblance to C programs. However, Perl incorporates many features that have their roots in other languages such as Lisp. These advanced features are not well understood and are rarely used by most Perl programmers, but they are very powerful. They can automate tasks in everyday programming that are difficult to solve in any other way. One of the most powerful of these techniques is writing functions that manufacture or modify other functions. For example, instead of writing ten similar functions, a programmer can write a general pattern or framework that can then create the functions as needed according to the pattern. For several years Mark Jason Dominus has worked to apply functional programming techniques to Perl. Now Mark brings these flexible programming methods that he has successfully taught in numerous tutorials and training sessions to a wider audience.

* Introduces powerful programming methods?new to most Perl programmers?that were previously the domain of computer scientists
* Gradually builds up confidence by describing techniques of progressive sophistication
* Shows how to improve everyday programs and includes numerous engaging code examples to illustrate the methods

User review
Haiku on a typewriter
Non-Lisp programmers, I think, fundamentally misunderstand the appeal of Lisp, and why it is superior to other programming languages. Lisp is not superior because it offers higher order functions or any of the other things, it is superior because it does not get in the way: Lisp meets problems, rather than forcing problems to meet it.


This book serves two purposes: on the one hand it helps Perl programmers to move the center of gravity of their solutions further away from Perl, and a bit closer to their problems (quite a bit closer, actually), and on the other it provides a good introduction to Perl for Lisp programmers, and for that it gets an effortless five stars.


I suspect that it is maybe more successful for the second purpose than the first: a relaxed read for someone who knows what a closure is, but for someone who does not know what a closure is, most likely not a relaxed read. Further, anyone who masters the contents may find that his newly refined coding style is somewhat caviary to the general, something that would be a problem in any IT/ORG that I have experienced.


Small quibbles: A Pratt Precedence parser might have fitted the discussion better than the recursive descent parser that is developed, and a deeper discussion of the ways that Perl nevertheless falls short of Lisp, instead of just a passing reference to Norvig's list, would actually have made the arguement stronger - for instance you cannot add a macro facility similar to that of Lisp to Perl, and you cannot add the general continuations available in Scheme (or the limited continuations that Graham, e.g., shows how to add to Common Lisp).


In short, Higher Order Perl makes a good case for the the aesthetics of haiku, but one should understand that (in spite of what my cousin, Leontia Flynn, thinks) it is haiku written with a typewriter, not with a caligrapher's brush.


P.S. One minor repeated irritation for me was the way the author, in spite of everything, insisted on emphasising how input functions should read input files one line at a time. This is a bizarre mindset holdover from the seventies, that is inconsistent with the general argument of the book: if you have a computer with hundreds of megabytes, or gigabytes, of core, then the efficient way to read a text file is not line by line, but in one go.



User review
good book, for people with computer science background
good book, for people with computer science background! otherwise a little hard to understand the algorithm.

User review
Solving Problems using Perl in a Functiona Programming approach
Functional Programming is a programming paradigm that is very useful and must be part of the tool-arsenal of any software engineer and developer along with the well known Object Oriented Programming, the procedural programming approaches and the less known Aspect Oriented programming.


HOP gives an easy access to Functional Programming for Perl developers. The book is written in a personal manner and is fun to read and sometimes even funny. It is definitely not a dry factoid book.


The examples in the book are useful not only as they demonstrate nicely the need and power of functional programming but also due to their practicality for seasoned programmers.


I felt the influence of books such as `Structure and Interpretation of Computer Programs` by Abelson and the Sussmans` on HOP, and it was very nice and helpful to read MJD's recommendations for other good readings on the subject.


I got to reading HOP cover to cover, several times. I attempted to solve myself the problems raised in the book and found the process of doing that enjoying and rewarding. Reading on the commentary and approach suggested by MJD was in many ways enlightening. You can find some very amusing footnotes in the book that makes it yet more fun to read.


MJD gives a refreshing view on Perl programming, and shows that tasks that can be very complicated and hard, or computationally intense can be turned `inside out` and handled in a functional programming approach that simplifies understanding, simplifies the solution and also simplifies the implementation.


MJD presents a very useful introduction to functional programming using Perl using practical problems, and the nice thing is that even theoretical problems get practical usages when explained, which makes the reading fun, and worth the while for the non-theoretical people amongst the readers.


Though the book sometimes goes into detail or length too much for my taste, it does do a good job in giving the new view of the capabilities of a Perl programmer and the Perl programming language and how to use them.


I can testify that a lot of code that I got to see, write and maintain got a considerable `lift` and `upgrade` since me and other colleagues of mine read this book and used some of the wisdom that it offers.


There are many good and useful follow-ups to this book in the form of Perl modules on CPAN, articles and posts on the web and some book chapters. Look them up!


Also, if you feel that this functional programming approach is useful to you, you might want to probe further on this by reading Haskell, Lisp, Scheme (and other functional programming languages) literature. There are several `must read!` for these programming languages that will give you a very deep understanding and good skills in this programming paradigm. Also -- you will find, especially after reading HOP, that you now have the tools to easily and practically apply the functional programming principles that you learn in Functional Programming books to Perl and use it in your Perl code.


A few words on MDJ himself: I read a lot of what he wrote for Perl and used some of his modules and code that is available on the web. I had the pleasure of hosting him on the first YAPC in Israel and attend a 3 day course that he gave. He is a very smart person, a very nice person and an excellent teacher and a very good writer. If you don't already know MJD, this book is a very good way of getting access to some of the wisdom that this gut has to offer.


If you don't know which Perl books to buy and/or to read, know this: HOP is one of the books that you should read!


(It is funny that almost 3 years after I read this book for the first time and after reading some of the material before editing, as MJD offered in his private mailing list, I only now get to write a review on it. But still, it surely deserves a book review and a good mark too.)

User review
a must-read book for serious Perl programmers
I had originally planned to read Higher Order Perl and write my review immediately. When it became clear that I wasn't going to read HOP straight through, I figured I'd write a review when I was done. More and more, it looks like my reading of the last two chapters is being indefinitely delayed. I'm going to write what I think so far, while it's still more or less fresh in my mind.


Higher Order Perl was originally going to be given some fairly bland name, like `Handbook of Advancted Perl Techniques.` This would have been a spot-on (but uninteresting) title. HOP provides the reader with explanation and demonstration of techniques for problem-solving that are often overlooked. The examples are complex and detailed, but not byzantine, and they're built up slowly, piece by piece, so that each line of code's meaning and significance are made clear.


The title `Higher Order Perl` refers to the book's most central technique, functional programming. While many programmers understand how to abstract a specific solution into a more general one, Dominus helps the reader learn to push the envelope, abstracing generic solutions into extremely generalized solutions that can be applied to seemingly-unrelated problems. This is frequently done by the construction of functions that build functions that build functions -- and so on, functions all the way down. Instead of solving the problem in base, earthly Perl, the programmer produces Perl elements of a higher order which, operating in harmony, become all things to all people.Well, I'm hyperbolizing, but I think it would be hard for me to over-emphasize the value of techniques like closures, iterators, and currying. They are, in part, what make Lisp so powerful, and the marriage of Lisp's power and Perl's expressivity is a happy one.As for the writing, it is good. The language is clear and the material is well-presented. One should be cautioned, though, that the book is dense. Dominus is constantly pressing onward, explaining new techniques or new ways to apply already-explained techniques. I found myself reading each page carefully and deliberately, only to turn back to it a few pages later, to be sure that I understood how the new material was relying on the old. It made the book a challenge to read, but it was a thoroughly enjoyable challenge. I never felt so frustrated by a bizarre idea that I gave up or so bored with an over-explained one that I skipped ahead.


Finally, while the techniques that Dominus presents are powerful and advanced, the required knowledge of Perl is not particularly great. Because he clearly explains the key Perl concepts that he uses (especially closures and associated scoping issues), any competent programmer with a working knowledge of Perl should be able to put the ideas in Higher Order Perl to work.


For serious Perl programmers, Higher Order Perl is a must-read book.

User review
Functional programming meets Perl, this is something special
In a book market awash with teach-yourself-in-x-time-units, cookbooks, and API tutorials, Higher-Order Perl is ambitious. It attempts to bring functional techniques to Perl, leaning heavily on two language features: closures, and functions as first-class entities. To enjoy this book you need to be completely comfortable with Perl's syntax, there's lots of code to read and digest, and if you can't follow it, you're going to get lost very quickly, because there are lots of new concepts going to be thrown your way. There's no outlandish symbol table hacking, almost no object orientation, not even much use of modules, but it is in a very different style to most of the Perl you're used to.


If, like me, most of the programming languages you're familiar with are more closely related to C than Lisp, this is going to be a challenging read. It's going to take you a long time to get through this book, and it'll probably require re-reading (more than once) to fully get it all. This is a book bursting at the seams with ideas, beginning with recursion, and then onto caching, iterators, streams and currying. The last two chapters show how to apply these techniques to parsing and declarative programming, and they feel like mini books in themselves. Applications of the code range from classics like Tower of Hanoi, Fibonacci sequences and the Newton Raphson method, to more practical material including databases, tied files, and directory walking.


Part of the reason I enjoyed this book so much may be because I have no formal training in Computer Science, and of course, the likes of SICP and The Little Schemer contain similar concepts. But for someone without functional programming experience it's great to have it in Perl. Lispniks, MLers and Haskellites may well be familiar with these ideas already, but they should consider this an enticing 'gateway book' for Perl programmers. And Introduction to Functional Programming using Haskell just isn't as well-written as HOP. There's no hiding from the fact that if you want to fully appreciate the material here, you're going to have to work hard at it, but Mark Jason Dominus does a great job at stopping things from feeling like a textbook, and knows how to inject some wit into the proceedings without being distracting. Full marks to Morgan Kaufmann for the layout and overall production quality, too.


I keep The Camel, Perl Cookbook and Perl Best Practices on my desk when programming Perl. HOP is not going to be joining them (yet), it's not that sort of a book. Instead it's a very different intellectual pleasure and easily one of my favourite and most important Perl books (only PBP edges it out due to the latter's everyday practicality). If you're serious about Perl, you need to read this book. If you're serious about programming, ditto. Hell, this might just be a good enough reason to learn Perl if you don't already (admittedly, the chances of such a person reading this far are small).


HOP has set the bar very high. Python and Ruby authors, please step up!


Download this book!

Free Ebooks Download

No comments:

Post a Comment