Slashdot Mirror


The Perl Black Book

Reviewer Greg Smith here dissects a book aimed at programmers who want to add Perl to their stable of languages, but also useful to the Perl connoisseur. If your interest in Perl is more than casual -- especially if you're seeking practical code examples more substantial then in more introductory texts -- The Perl Black Book may be for you. (Read more.)

The Perl Black Book author Steven Holzner pages 1283 publisher Coriolis rating 7/10 reviewer Greg Smith ISBN 1-57610-465-6 summary Perl introduction and reference guide with lots of example code, targeted at those already familiar with another programming language.

The Scenario You already know C or BASIC or some other programming language, but there's some work you need to finish that none of these languages are the right tool for. Perhaps you want to create CGI programs. The Perl Black Book is an excellent choice to fill that need. It's a non-nonsense title that's focused on working example code in a form that will be familiar to those stepping from another programming language into Perl. What's Bad While the book is large, the scope of material it attempts to cover is considerably larger. Coriolis labels the book as intended for intermediate to advanced materials, and there's a minimum of introduction to general programming concepts. Non-programmers trying to learn Perl as their first language would be better served starting with a more introductory title and coming back to this book later. And while there are examples of things like object-oriented Perl and CGI, none of that material goes deep enough to really satisfy a serious student of those topics.

The most annoying thing about this Coriolis release is the introduction to most sections. Holzner spins these little stories about a Novice Programmer, his Big Boss, a Programming Correctness Czar, and other random characters to suggest why you'd need to know about a topic. I question the necessity of these motivational interludes, as they take up space, waste my time reading them, and offer very little in return. The kind of person I'd expect to be reading this book doesn't need a little story suggesting why you'd want to be able to format text when you print it; that this is a useful thing to learn is pretty obvious.

What's Good The Perl Black Book does an excellent job with the kinds of things intermediate programmers in the language are interested in. It's become the first place I turn to when looking for a snippet of Perl code to incorporate into a project. An example of why I like this book should illustrate why that is. I recently found myself writing a program that needed to ping a host and take some corrective action if that ping was unsuccessful. I would expect this is a straightforward request, and looked up "ping" in the index of O'Reilly's Programming Perl. This led to an example using the pingecho library call, which I typed in and found utterly unsuccessful at the task I was trying to accomplish. Looking up the same topic in the Perl Black Book, I found a section about twice as large on the subject. Holzner's example program worked just as I was expecting, and I went back to the rest of the application design without needing to focus for very long on the implementation details of this small piece. I've never liked the code in Programming Perl because the samples are all so terse. The Perl Black Book does a much better job in my view of providing programs long enough to demonstrate the appropriate Perl syntax for filling common needs. So What's In It For Me? Since so much Perl programming is done in the context of a CGI script, many chapters of this book are devoted to that topic. The majority of the material revolves around using Lincoln Stein's CGI.pm library. Much of this mimics Stein's own book, Official Guide to Programming with CGI.pm, but instead of the reference-guide approach, there's more of an emphasis on practical solutions for regularly recurring requests. Example code shows how to create Web counters, guest books, e-mailers, chat, shopping carts and other popular items. A single chapter also introduces usage of the older cgi-lib.pl to create basic HTML pages, but the main focus is definitely CGI.pm, using all of its abstraction. While the CGI section is substantial, considerably more so than the average Perl programming book, there are a number of areas that will need supplementation before you'd want to create a production system. The discussion of the taint mechanism for security is a bit weak, and the basic information about cookies and hidden fields doesn't really give an impression of how to build a larger-scale Web application and properly save state along the way. That is, to be fair, outside of what I'd expect a Perl book cover, and it's only because most of the other material so is helpful that these weaker areas stick out.

The Perl Black Book provides plenty of the kind of examples I'm looking for when writing a Perl program, and it's large and comprehensive enough to cover most areas in sufficient depth. The real question is whether it meshes well with your approach to the language. Holzner is obviously aiming this title at someone who is learning Perl as their second, third, or nth programming language. The samples avoid relying on Perl-specific idioms in most cases. Personally, I find this approach refreshing, as programs I write in Perl frequently end up getting ported to another language later, or co-opted by another programmer who isn't as familiar with the language. So for me, writing Perl that embraces too heavily Perl's language specific features leads to portability and maintenance problems later. For example, when I read through Randal Schwartz's Learning Perl, by page 11 he's already using regular expressions to match strings. I find myself wanting to use a more traditional substring/comparison operation in that context instead, because I know that code will port to any other language easily, and any competent programmer will understand what I'm doing regardless of their familiarity with Perl. Holzner understands that mindset, and writes his prose and programs accordingly; witness the following tip from P291:

"The line ++@_[0]; is a cute example of why nonprogrammers think Perl is obscure. Try showing that line to your grandmother and explaining that you write stuff like that for a living."

If you look at the example above and think that's a nice way to use Perl, by all means stick to the traditional books on the topic from Schwartz, Larry Wall and Tom Christiansen. But if you think that line is unnecessarily cryptic, I think you'll be impressed with how little material like that is inside Holzner's Perl Black Book. It doesn't try to embrace "The Perl Way," but it is a quite useful resource when you're on a short deadline to deliver a working program and don't have time to absorb too much of the language to do it. I'm buying a second copy for the office this week because mine keeps walking away, and there's no greater recommendation for a computer title than noting that copies of the book disappear regularly.

Purchase this book at ThinkGeek.

Table of Contents
  1. Essential Perl
  2. Scalar Variables and Lists
  3. Arrays and Hashes
  4. Operators and Precedence
  5. Conditional Statements and Loops
  6. Regular Expressions
  7. Subroutines
  8. Formats and String Handling
  9. References
  10. Predefined Variables
  11. Built-In Functions: Data Processing
  12. Built-In Functions: I/O
  13. Built-In Functions: File Handling
  14. Built-In Functions: Interprocess Communication
  15. Standard Modules
  16. Perl/Tk--Windows, Buttons, and More
  17. Data Structures and Databases
  18. Debugging and Style Guide
  19. Creating Packages and Modules
  20. Creating Classes and Objects
  21. Object-Oriented Programming
  22. Internet and Socket Programming
  23. CGI Programming: CGI.pm
  24. CGI Programming With Other Popular Package
  25. CGI: Creating Web Counters, Guest Books, Emailers, and Secure Scripts
  26. CGI: Creating Multiuser Chat, Server Push, Cookies, and Games
  27. CGI: Creating Shopping Carts, Databases, Site Searches, and File Uploads
  28. Handling the Web in Code

5 of 41 comments (clear)

  1. Re:Regexps and... what DOES ++@_[0]; do? by ruud · · Score: 3
    For this construct, perl -w reports:

    "Scalar value @_[0] better written as $_[0]"
    --

    --
    bgphints - internet routing news, hints and ti
  2. Huh? by alleria · · Score: 3

    The samples avoid relying on Perl-specific idioms in most cases. Personally, I find this approach refreshing ...

    I find myself wanting to use a more traditional substring/comparison operation in that context instead, because I know that code will port to any other language easily, and any competent programmer will understand what I'm doing regardless of their familiarity with Perl.

    [Holzner's Perl Black Book] doesn't try to embrace "The Perl Way" ...

    I see. So it's a book for programmers from other languages who don't really want to learn Perl. That's too bad. Can you imagine a ANSI C++ book that decided that because the STL was rather idiomatic and specific to C++, that they simply weren't going to cover it?

    And what's this about having 'any competent programmer will understand ... regardless of their familiarity with Perl'?? Excuse me: when I write in English, I really don't try to use the Latin subset of English that happens to be the interesection of all Latinate languages, just so that a French speaker can understand my English. So why, when I decide to write Perl, should I write for a Smalltalk or Eiffel programmer?

    By writing Perl with syntax and conventions from other language (so that you can port), you defeat the entire purpose of using Perl as your language.

    I mean, why WOULD you use Perl, if you were just going to write stuff that looks like a bad approximation of C++ or Java code, and then just port it later? Sounds like the worst of both Perl and your other language, in that case!

  3. Re:Regexps and... what DOES ++@_[0]; do? by dlc · · Score: 4

    @array[0] is a one-element hash slice ($array[0] is the first member of @array), and the pre-increment only does the obvious thing because it is a one-element slice. This is bad programming style, and horribly confusing, not idiomatic Perl. Idiomatic Perl is about saving typing and running time by using shortcuts, and this does neither (array lookups are slower than scalar lookups, as well as the same number of characters to type). Without the context for this statement, however, who knows what it's author meant.

    By the way, ++@_[0,1] (a two-element hash slice) increments only $_[1] (the last value).

    darren


    Cthulhu for President!
    --
    (darren)
  4. Perl-thinking by RubiCon · · Score: 3

    I've always been a fan of Perl because it maps to the way I think - when I concoct solutions in my head, I think in the same kind of atomic units that Perl (and Tcl, for that matter) uses.

    The problem here is that writing a Perl book designed specifically to shoehorn a C-thinking (or worse yet a VB-thinking) mind into a Perl-mindset is never going to hit the mark. Ultimately, to make that transition, you have to be willing to ditch a lot of the coding intuition that you've built up.

    The Camel Book understands this and starts by teaching the lesson of TMTOWTDI and by belting you with regexps as soon as it possibly can. A book that tries to mollycoddle and tell you you don't need rethink your coding strategy is probably never going to tap the full potential of Perl.

    Oh, and I'd hope no Perl hacker worth their salt has ever used ++@_[0]; - but then, I suppose

    s/([\000-\037"&<>\177-\377])/'&#'.ord($1).';'/ge;

    is confusing enough for non-Perl-thinkers. ;)

  5. Re:The Perl Way by nakaduct · · Score: 3
    there are actually many instances where index() and substr() work much better than regexes

    Yes, though you failed to provide an example. I benchmarked the example you gave (using a 16000-line passwd file as the source), and (higher numbers are better):

    • index: ... @ 160060.30/s (n=536202)
    • regex: ... @ 165649.72/s (n=499710)
    • split: ... @ 152474.50/s (n=536202)

    The code: (use view source to get the line breaks and diamond):
    #!/usr/bin/perl use Benchmark; @pw=; # give source file name on cmd line timethese -3,{ 'index' => 'my @n=map { substr $_,($a=index $_,":"),index($_,":",$a+1) } @pw', 'split' => 'my @n=map { (split ":",$_,3)[1] } @pw', 'regex' => 'my @n=map { (/^[^:]*:([^:]*):/)[0] } @pw' }

    ... all within a few percent, and the Perl Way solution (using pure regexes) is still faster than the {large,inflexible,confusing} index/substr method. Getting the fourth field means the logic has to change (and, I'll wager, the performance "gain" evaporates). Handling escaped delimiters is a major pain. And so on.

    In my experience, when choosing between the Perl way and an old-school algorithm-textbook way, the Perl way:

    • Will run in the same time complexity (maybe *lgN, especially when hashes are involved), and with a negligible constant cost.
    • Will be easier to write.
    • Will be easier to read.
    • Will be highly adaptable to changes, often without rewriting -- regexes written with ambiguity in mind are particularly good at handling changes in input formats.
    • Will use more memory. This really is Perl's Achilles' Heel. The only time I've had to use tactics like you describe is when iterating over huge input files (~10M lines) -- if the garbage collector isn't perfect (and it isn't) the accumulated leakage will kill your program.

    So, forget about 1% performance variations. Concentrate instead on the benefits of solving problems in half the time, and of having written code that someone less-brilliant than yourself can maintain.

    cheers,
    mike