Slashdot Mirror


Randal Schwartz's Perls of Wisdom

r3lody (Raymond Lodato) writes "Anyone who has been working on the *nix platform has had a brush with Perl, the scripting language whose acronym (depending on who you ask) could mean Practical Extraction and Report Language, or Pathologically Eclectic Rubbish Lister. In either case, there is a distinct difference between learning to use Perl, and learning to use it well. In my opinion, the best way to learn any language well is to see how others have used it to solve problems. One of the foremost experts in the use of Perl, Randal L. Schwartz, has been writing columns since March of 1995 on the use of Perl in the real world, and has provided us with 6 books and over 200 columns with many examples on how Perl is used." Read on for the rest of Lodato's review. Randal Schwartz's Perls of Wisdom author Randal L. Schwartz pages 350 publisher Apress rating 7/10 reviewer Raymond Lodato (rlodato AT yahoo DOT com) ISBN 1590593235 summary A dated compendium of the best of Randal's Perl columns

Perls of Wisdom is a collection of 65 selected columns from Linux Magazine, Unix Review, and the now-defunct Web Techniques magazines, written between May 1995 and July 2004. In each column, Randal discusses some problem that he had to solve, or that someone else needed help in solving. He carefully discusses the problem, and then shows the Perl code needed to resolve it. Many of the columns are complete applications that can be run (with minor modifications) by the reader. (The listings are also available from the apress.com web site.) Each column has been reproduced as it was written in the original magazine, with "Randal's Note" prepended. Therein lies this book's best feature and greatest flaw. Allow me to explain.

When I first picked up this book, I had only read a couple of Randal's columns (from Web Techniques), and saw that he wrote tutorials of proper Perl usage. He also relies on the wealth of modules submitted to CPAN to leverage a solution. After all, why reinvent the wheel? I expected to see more commentary on the reasons behind choosing one solution over another, or insights into the inner workings of Perl itself. I more or less got what I expected. For example, the first column reproduced in the book (It's All About Context) explains why, when someone used my ($f) = 'fortune'; instead of my $f = 'fortune'; he got in trouble with the law (see the book to understand the legal issue). The first form only retrieves the first line of the output of the fortune program, while the second form retrieves the entire output. Little items such as scalar versus list context can trip many Perl coders.

The first chapter (Advanced Perl Techniques) does give you many tips and insights like the example I just gave. All but two of the twenty columns are little tutorials on the ins and outs of handling the commonplace day-to-day issues that Perl can address with ease. Some delve into more obscure topics, such as the difference between shallow and deep copies of structures, and Perl's Taint mode. Two columns contain complete programs. One extracts the text from the man pages and determines their "fog" index (a measure of readability). The other creates a mirror of files needed by CPAN.pm to install new modules. For each program, Randal gives the entire listing as well as an almost line-by-line description of how it works. Each column is written in a conversational style that is easy to read, yet doesn't talk down to you.

The following chapter is comprised of seven tutorials on the various aspects of searching, sorting, and formatting text. In addition to describing the creation and compilation of regular expressions, Randal also discusses formatting and the nifty "Schwartzian Transform" (Perl's map-sort-map idiom for sorting on almost anything) which was named for him, but not by him. While some of the information is a little long-in-the-tooth (the column on Text-Processing was written shortly after Perl 5 was released), it's all interesting and educational nonetheless.

Chapter 3 starts refocusing the use of Perl to web sites. This chapter discusses HTML and XML processing in six little columns. He shows how to generate a web page index, producing a web page calendar from a file of events, and parsing XML to retrieve the data within. He also includes a lesson on how to use Perl to compare two arrays to create an HTML-formatted difference table.

The next chapter demonstrates that Randal has spent a lot of time working out ways to update and improve his web site. It covers the intricacies of CGI programming in Perl. All but one of the fifteen columns are complete programs (again, available from apress.com) with line-by-line commentary. The programs do implement mostly worthwhile functionality, but each column was pretty much "I had this problem, so I wrote this program. Lines 1-3 do this; lines 4-5 do that, etc." Granted, some of the programs are pretty nifty (check out how he automatically keeps track of the "What's New?" pages), but the reading of one program after another started to become stale.

The final chapter is titled "The Webmaster's Toolkit," consisting of fourteen programs and three tutorials. Randal covers diverse web server background topics such as creating a light-weight load balancer, random links, and forcing users to enter through the "front door." There are also instructive techniques for throttling your web server's usage of the processor (a necessity at the time for Randal, as his web site was co-resident on a server with others), and calculating download times.

In its entirety, Perls of Wisdom contains 65 columns, split roughly half-and-half between tutorials and fully commented programs. More than half of the columns show that Randal uses Perl for web processing more than for general scripting, data reduction and reporting. His tutorial articles are top-notch, but I have a quibble over his program articles, which are somewhat dated. There were a number of prefaced notes to the effect that today he'd do it differently with some new feature or CPAN module. I really wish he had actually updated the column to show the new coding techniques. The original code is interesting in the historical sense, but I wanted to see nuggets of Perl wisdom for me to use in my daily job. The writing style is fine; the bits of insight are useful, but many of the programs are too specific to problems you or I may never see, and were solved in code that's showing its age. I'm glad I got to read the book, but I think it only rates a 7 out of 10.

You can purchase Randal Schwartz's Perls of Wisdom from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

8 of 282 comments (clear)

  1. CGI programs by JaxWeb · · Score: 5, Informative

    I first learn Perl with the aim of creating dynamics webpages. I learnt from the tutorial Picking Up Perl - this is great and taught my most I needed to know with regard to the language - but it didn't teach me how to use it for websites.

    I picked up from code lying about how to read and write files, get post/get data, and so forth, and slowly built up into quite a good Perl programmer (I suppose. Not amazing, but quite fluent). This wasn't easy though and was slow. Why? I never got taught, all in one place, how to do that. I think this is what this book is trying to do - but with a much wider range than just CGI programs (although it doesn't seem to neglect it, either).

    I tried to write my own tutorial for using Perl in webpages to try and help. I'm not going to link to it here though, because it is quite terrible (I was 14 when I wrote it).

    After learning Perl, and being able to use it, there is always using the standard librarys. For this, PerlDoc has been so helpful to me.

    --
    - Jax
  2. Link to Randal's Articles by Matt+Perry · · Score: 5, Informative

    Since there wasn't a link to Randal's collection of articles I'm providing one here. There's some excellent stuff in there.

    --
    Slashdot: Failed Car Analogies. Amateur Lawyering. Anecdote Battles.
  3. Tip #1... by MoeDrippins · · Score: 5, Funny
    --
    Before you design for reuse, make sure to design it for use.
  4. Re:Reading Perl code? by eln · · Score: 5, Insightful

    A good programmer will write readable code in any language he's writing in. Yes, there is a considerable faction who delight in making Perl code as obfuscated as possible, but it certainly doesn't have to be that way. C allows you to write some pretty ugly code too, but that doesn't make it a good idea to do so.

    The TIMTOWTDI ethos is ultimately what makes the language as useful as it is. The more ways there are to do things, the more things you can do with a little creativity. The Perl language is abstracted, as all higher level languages are, but it's forgiving enough to allow you to do things that may not be possible in other languages without a great deal of pain.

    The myth that all Perl code is unreadable is preposterous. I've written several very complex pieces of code in Perl that are not any more difficult to comprehend than any piece of C code designed to do the same thing, and often much less so. The phenomenon of obfuscated code is a result of poor programmers (and a pervasive subculture), not of the language.

  5. Perl doesn't kill readability... by jqh1 · · Score: 5, Insightful

    People kill readability (with Perl).

    Seriously, choose the right tool for the job. When we've got a sys-admin level scripting task, and someone can go in and knock it out in a half hour (or less) with a few lines of Perl, who can say that's bad? I'm currently wading through a bunch of heavily patternized java that pulls checkin logs from a scm system and updates an issue tracking system as part of the build process. It's taken me *days* to begin to "grok" what's going on in the many associated xml config files and bizarre string handling approaches that were used in this undocumented hack. I'll replace it with probably less than 150 lines of Perl, and someone else will happily (and much more easily) maintain it. So there!

    At the same time, we've got > 25 developers distributed around the world working on a big commodities trading app -- java works pretty well for that.

    --
    who's moderating the meta-moderators?
    1. Re:Perl doesn't kill readability... by ajs · · Score: 5, Interesting
      "People kill readability (with Perl)."

      As they do with C, C++, Java, Snobol, Forth, APL, C#, etc, etc.

      Half of the people programming are below-average programmers. Bad programmers can make life HELL in C, and by the same token good programmers can make life quite easy in Perl.

      That said, Perl gets a bad rep, not because good code is hard to read, but because a) bad code is more common in any language which is easy to learn and b) Perl has several features which people mistake for non-readability (that is, non- or inexperienced Perl programmers assume that code is hard to read because they don't know Perl and see these things which scare them):

      • Regular expressions - This is a common feature in almost all languages these days, but the ease with which they are integrated into Perl syntax makes them more common in Perl programs. Of course, you have to be careful about the amount to which you let such constructs take over your code, but Perl was the one to introduce whitespace formatting and comments into regular expressions for just this reason.
      • Typing glyphs - Many programmers from the C/Pascal/Fortran - derived world take exception to the prefix-characters in Perl. Some Perl programmers find it hard to read C code that uses subroutines, complex data structures and simple types without any indication of what's being accesed or how. It's a matter of time and exposure on BOTH sides, and being a programmer in both worlds, I can tell you that both are equally readable with sufficient exposure.
      • Context sensitivity - Perl's context sensitivity spans every level from the way the tokenizer works all the way up to the handling of large-scale data structures. This is the nature of a language designed by a linguist. It "reads well", but only when you start trying to read it like a spoken language, and not a mathematical code. Software source code has always been somewhere in the middle-ground between those two extremes, and it's jarring at first that Perl moved the line so much, but give it a while and you find that it's much easier to think and communicate complex ideas (by complex, I mean cognitive complexity, not code complexity) in Perl than in any other programming language.
      • Weak typing - This is a matter of religion, but I'll make a footnote of it anyway: Perl is weakly typed, and that frustrates many who are used to strong typing. Neither is better or worse, though the fact that Perl 6 will (as Common LISP has done for quite some time) give you both is a boon, I think.

  6. Re:Perl and work by gurps_npc · · Score: 5, Insightful
    You made an error that I think a lot of people make.

    It is not how long it takes you to understand a perl script of X length that is relevant.

    What SHOULD be the important data is: Whether it would take you MORE time to understand a C or Java program that does the same thing.

    I have seen cases where people complain that it takes them a week to understand a Perl Script of 500 lines. So they write a new program in C, that does the same thing in 5,000 lines. Which then takes me 8 days to understand.

    One of Perl's image problems is that because it does so much with so little, people underestimate what the tiny program does and therefore get frustrated when it takes them more time to understand than a C program of the same size, even though the C program does 1/10 what the Perl one does.

    --
    excitingthingstodo.blogspot.com
  7. Re:Not an acronym! by teknomage1 · · Score: 5, Informative
    From perlfaq

    What's the difference between "perl" and "Perl"?

    One bit.

    Oh, you weren't talking ASCII? :-) Larry now uses "Perl" to signify the language proper and "perl" the implementation of it, i.e. the current interpreter. Hence Tom's quip that "Nothing but perl can parse Perl." You may or may not choose to follow this usage. For example, parallelism means "awk and perl" and "Python and Perl" look OK, while "awk and Perl" and "Python and perl" do not. But never write "PERL", because perl is not an acronym, apocryphal folklore and post- facto expansions notwithstanding.

    --
    Stop intellectual property from infringing on me