A new Unicode appendix

I’ve added a new appendix to Learning Perl to handle all of the Unicode stuff I was having difficultly integrating into the other chapters.

Our goal has always been to present just the information you need without getting into distracting details. The problem with Unicode is that there are a lot of distracting details. Not only that, you have to learn some things in tandem. We can’t talk about Unicode strings without introducing strings, but at the same time, we want to start with Unicode as the basis for strings.

I wanted to have a lot of that stuff in the Strings chapter, but a lot of the Perl Unicode stuff lives in modules. We do talk about modules later in the book, but I want to use some of them earlier.

Any beginning book is going to have this problem. You need to ignore some stuff to at least get started. As such, I gave up on trying to cram all the Unicode stuff into the chapters and put most of it into a new appendix. This also means that if people want to ignore some of the Unicode stuff, which I don’t recommend, they can. But, they shouldn’t. So, read the whole book, even the appendices!

when(), Try::Tiny, and autodie

I’m working on Chapter 17, which is the catch-all chapter for topics we think that segue into the other books in the Learning Perl series. Although Mastering Perl has an entire chapter on catching and reporting errors, we want to at least survey the topic in Learning Perl.

The first edition of Learning Perl noted that eval existed and gave a couple of examples, and in each subsequent edition the discussion became more involved.

Starting with the fourth edition, we devoted a chapter to using Perl modules, acknowledging the fact that Perl’s greatest feature is CPAN. In that edition, it was fairly late in the book. In the fifth edition, we moved that chapter toward the middle of the book. In each case, this means that we can then use Perl modules, whether from the Standard Library or CPAN, for the rest of the book since we’ve covered the idea of using modules. Our goal is always to cover any topic before we use it.

Since the sixth edition also covers modules, we can use it when we talk about catching errors. That means that we can talk about autodie, the pragma that became part of the Perl core in 5.10.1, and Try::Tiny, which is not a core module. We might have covered autodie in the fifth edition, but we only covered up to Perl 5.10.0. Paul Fenwick just barely missed the cut-off.

So, while working on the eval section, I was playing with some examples. I covered eval, Try::Tiny, and autodie separately, but I was wondering what would happen if I combined them. Could try and autodie cooperate?

I started with the example from the autodie documentation:

eval {
   use autodie;
   open(my $fh, '<', $some_file);
   my @records = <$fh>;
   # Do things with @records...
   close($fh);
};

given ($@) {
   when (undef)   { say "No error";                    }
   when ('open')  { say "Error from open";             }
   when (':io')   { say "Non-open, IO error.";         }
   when (':all')  { say "All other autodie errors."    }
   default        { say "Not an autodie error at all." }
}

Then I added Try::Tiny and started playing around with it:

use 5.010;

use autodie;
use Try::Tiny;

my $filename = '/does/not/exist';
try {
  open my $fh, '>', $filename; # still dies on error
  }
catch {
  when( 'open'  ) { say 'Got an open error'; continue; }
  when( 'close' ) { say 'Got an open error'; continue; }
  when( ':io'   ) { say 'Got an io error';   continue; }
  };

The output is not what I expected. It does what it looks like it should do:

Got an open error
Got an io error

I was surprised that this worked, and that it worked without a warning. That when is outside an official topicalizer (something that sets $_, such as given or foreach ).

That’s one of the interesting parts of writing a book. To properly research something, we think about the different ways we might combine things and especially how things might break. When we’re teaching, we’re going to run into all sorts of crazy syntheses of the topics. With a bit of experience, we can anticipate some of that, and when we do we discover some interesting things like this.

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: