Updates to Chapter 4, “Subroutines”

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

There’s not much to write about Perl subroutines that we haven’t written before, but that doesn’t mean that this chapter gets a pass in the update. This is only chapter 4, so it’s still early in the book. Up to this point, we have only covered the basics of Perl scalars and arrays. Once we get into subroutines, we start to talk about scoping variables to a block, and it’s here that we introduce lexical variables.

Once we show of my, we can tell people about strict. Still, that’s nothing new. However, since the last time we wrote about strict, it was something that you had to enable on your own. Perl 5.12 added the feature that you’d get that for free by requiring the version of Perl.

Before Perl 5.12:

use 5.010;
use strict;

Starting with Perl 5.12:

use 5.012; # strict for free

We could tell them how to turn it off, but we still won’t do that until Intermediate Perl.

3 thoughts on “Updates to Chapter 4, “Subroutines””

  1. Does this chapter mention that subroutines can be assigned to scalar variables and passed around as arguments, just like other values? Back when this book was first written that was an advanced concept, but functional languages are more prevalent today and it might be good to show that Perl is just as functional as the rest of them. I think a simple example that doesn’t get into the details of references, closures, or currying would suffice at this point. Perhaps an example of passing a callback function to another function, or a Visitor pattern example. Those techniques are simple but can still useful, even for sysadmin-style scripting.

    1. Learning Perl is the bare basics for beginners. Once they get the hang of the stuff in Learning Perl, people can move on to Intermediate Perl where we talk about references, including subroutine references. To get even fancier with closures, there’s a lot in Mastering Perl. Anyone who wants to go the functional route, however, should dive into Higher-Order Perl.

  2. I’m suggesting a middle-ground, where the basic concept is introduced in the introductory book (which is all many Perl users will ever read) without making the concept impenetrable and scary for beginners. I’ve been programming in Perl for 15 years and consider myself an expert in most programming styles, but HOP is still a conceptually difficult read for me. It was worse when I first read it, before I understood the simpler and more practical aspects of using subroutine references. And most of that understanding I had to get from the Javascript world and jQuery, because it’s not well-provided in the Perl world. When I saw you’re working on a new edition to Learning Perl, it occurred to me that this is an opportunity to provide some of that basic practical instruction.

    BTW, thank you (and the other authors and everyone else involved) for putting out an updated edition. I’m really happy to see the renewed attention Perl is getting to let those outside the community know that we’re not just surviving, but thriving.

Comments are closed.