Updates to chapter 17, “Advanced Topics”

[This post notes differences between the fifth and sixth editions.]

Chapter 17 of Learning Perl is a catch-all chapter at the end of the book. We cover some “advanced” items to pique the interest of the reader and to segue into Intermediate Perl. Mostly, this covers grep, map, and eval.

Since we also cover Perl modules, we expanded this chapter a bit. The list operators are nice, and since list operations are most of many programs, so we should at least mention List::Util and List::MoreUtils.

The eval is the minimum for dealing with errors, and it also provides quite a bit of trouble as it handles $@. So, we also introduce Try::Tiny. We don’t say much about it, but anyone who makes it this far at least finds out that it exists. While we are at it, we might as well throw in autodie, especially since it’s part of the Standard Library.

Updates to Chapter 15, “Smart matching and given-when”

[This post notes differences between the fifth and sixth editions.]

We published Learning Perl, 5th Edition in 2008, meaning that we were writing it toward the end of 2007. Perl 5.10 had just come out after being years in development. We just had a peek at the final features as the book was going to press, so the 5th Edition covers Perl 5.10.0. That point release is important, because Perl 5.10.1 changed the smart match in one significant way; it’s now not commutative. We covered smart matching and the new given-when in Chapter 15, which we need to slightly update.

As people started to use Perl 5.10, they realized there were many problems with the new smart match operator, ~~. The smart match looks at its operands and then does something special based an what it sees. It doesn’t have to do the same sort of task for every set of operands. For instance,

$fred ~~ $barney;  # $fred eq $barney

In Perl 5.10.0, perl didn’t care which side the operands were on. The smart match was supposed to be commutative, meaning that $n ~~ $m should be the same as $m ~~ $n, where the operands are flipped around. That doesn’t quite work out because the smart match is a little too dumb about strings and numbers, so these were the same thing because the order of the operands doesn’t matter:

if( 1 ~~ '1one' ) { ... }
if( '1one' ~~ 1 ) { ... }

Both of those are false in Perl 5.10.0 because each does a string comparison, even though one of them is a number that perl must convert to a string. That means that you couldn’t use a smart match if you wanted a numeric comparison. This is important because the when in uses $_ as the lefthand side of the implicit smart match and you specify the righthand operand:

given( $number ) {
    when( 4 ) { ... }
    }

Since you put a number there, you’d think that you’d get a numeric comparison, but you don’t. That’s a problem. Since Perl 5.10.1, the order of the operands matter, and you basically have to consult the table in perlsyn‘s section on the “Switch statement”.

This change doesn’t affect too much of the text in Chapter 15 because we shied away from some of the more complex examples. Our code still works, and we hope that people will skip over Perl 5.10 to at least Perl 5.12, but we never know. We’ll still have to add some warnings about that particular release of Perl.

My notes for the updates to the Llama

I put together some of our notes for an update to Learning Perl, 6th Edition. That’s not a promise that all of these will make it into the book, but it’s a list of the topics that we think should be important to the beginner Perler.

One of the biggest changes, structurally, will be our integration of Unicode, which Perl has become quite nice at handling since we last wrote Learning Perl. Obviously that will be a large part of the chapter on input and output, but it can affect much earlier chapters, too.

We have a special post to track new features in the recent versions of Perl, which you might like to read while you wait for the next edition.

  • Update for Unicode throughout
  • updates to pack
  • bring back the chapter on one liners?
  • Perl 5.10
    • defined-or, //
    • new smart matching rules
    • \K escape in s///
    • The autodie pragma
  • Perl 5.12
    • Unicode interpretation of \w, \d, \s
    • The ... operator
    • Implicit strictures
    • Y2038 compliance
    • \N regex for “not a newline”
    • each on arrays
    • when as a statement modifier
    • delete local
  • Perl 5.14
      • /a, /d, /u match flags
      • /r on s/// and tr///
      • given and do
      • \o{...} for octals

New features in Perl 5.x

We’ll be updating Learning Perl for the new features since Perl 5.10.0. I’ve been tracking those new features over at The Effective Perler, so you can read about them there. Not every new feature will make it into Learning Perl since we only aim to cover those that are important for beginners. However, since those features profiled at The Effective Perler are the important ones, many of those will show up in the new edition:

They use subversion, we use git

This isn’t true anymore. The O’Reilly Atlas system now uses git!


O’Reilly Media’s DocBook build system lives in a subversion repository, and that’s just fine, but we like to use git. Subversion is more than adequate for a book. We’re not really dealing in code, and we don’t really branch a book chapter. There’s already a lot of social control built into the process. One author says “I want to work on regexes” and another says “I want to work on subroutines” and off they go to work on those separate chapters. Since the outline is mostly set in stone (but one of those really soft ones, not like Bedrock), we don’t have to move a lot of files around or guess at cross references. This means that we don’t need excellent branching and merging support. Continue reading “They use subversion, we use git”