What is Modern Perl?

After a gap of twelve years, a new edition of Programming Perl (affectionately known as “the Camel book” to its many fans) was published earlier this year. That came as quite a surprise to many people who had given up on seeing a new edition. ¬†What has changed in the new edition? Does the book now cover Modern Perl? The answer depends on what you mean by the term “Modern Perl”.

The term Modern Perl can mean a couple of things to different people. Sometimes it means those two different different things to the same people. It depends on context. That’s a joke for the Perl programmers.

The first, and probably the most common meaning of the term is as a catch-all description for the big CPAN projects that have transformed Perl programming over the last five or ten years. Where Perl programmers once painstakingly constructed classes and objects using blessed references, we now use Moose. Where we once talked to databases using DBI, we now use DBIx::Class. Where we once reinvented wheels every time we wrote a CGI program we now use powerful web frameworks like Catalyst, Dancer or Mojolicious – all of which use PSGI to make it simple to deploy our application in various environments.

That’s not to say that the older approaches no longer work. Perl has a long tradition of backwards compatibility and many of these newer tools build on the older ones. A Perl class built with Moose will be exactly the same blessed reference as a manually constructed one and DBIx::Class still has plain old DBI lurking at its core. So using the old tools still works fine, but using the newer tools (the Modern Perl tools) will make a programmer more productive and a program easier to maintain.

So that’s what many people mean by Modern Perl a lot of the time. And is that covered in the new version of the camel book? Well, no. The camel book has traditionally covered the core of Perl and has largely ignored both CPAN and the practical uses of Perl. Which means that these Modern Perl tools largely fall outside of the book’s scope. The camel book doesn’t cover either database connections or web programming, so DBIx::Class and PSGI never get a mention. Object oriented programming is covered in chapter twelve of the book, but that’s almost all about the traditional way of writing classes in Perl. Moose gets a brief mention at the end of the chapter as an alternative approach which is well worth investigating.

If the camel book doesn’t cover these big Modern Perl projects, then was it really worth publishing the new edition? Is it worth buying a copy? Well, yes, because there’s the other kind of Modern Perl which is covered in some depth.

The third edition of the camel book was published when Perl 5.6.0 was the current version. We’re now on Perl 5.16.0. Between the two there have been about twenty stable releases of Perl. Over those twelve years a lot has changed in the core of Perl. It’s been slow and incremental and people who haven’t been keeping up with the perldelta manual page ¬†might not have noticed, but there have been a huge number of improvements. Little changes that make your Perl programming live easier, your Perl programs easier to write and easier to maintain. Here are a few examples.

  • The state keyword allows you to to have variables that are scoped to live within a subroutine but which maintain their values between calls to the subroutine
  • Perl now has a real switch statement
  • File handles can be stored in scalar variables – and can therefore be automatically closed when a lexical variable goes out of scope
  • Perl’s support for Unicode is equal to, if not better than, that in any other programming languages
  • Regular expressions have gained many new features

This list just scratches the surface. Core Perl has gained many more new features in the last twelve years. There are also the many new modules that been added to the core distribution – very useful things like Test::More, List::Util, Time::Piece and autodie.

The Perl of 2012 is substantially different to the Perl of 2000. But many people don’t seem to realise that. Because the definitive Perl reference book hasn’t been updated, it’s easy to believe that Perl itself hasn’t been updated. On various mailing lists and web sites I constantly come across people who write Perl like it’s still 2000 (or, in many cases, even earlier). Their code still works, of course, but because they haven’t been keeping up with the way that Perl has evolved they aren’t using many tools and techniques which would make their lives easier.

That’s what I see as the most important reason for the new edition of Programming Perl. The definitive Perl book is now up to date with the way that the best Perl programmers now program Perl. No, it doesn’t cover DBIx::Class or PSGI, but that’s not important. It covers hundreds of small ways that Perl has changed and improved over the last twelve years. Programmers buying this edition will have their eyes opened to a whole new world of Perl programming. Their Perl programming lives will become easier. They will be using Modern Perl.

If you’re interested in hearing more about Modern Perl (in both of its meanings) Dave Cross (@davorg) is running a low-cost one-day course in London on 6th October. See Perl School for more details.