Why we teach the subroutine ampersand

In Learning Perl, we tell readers to use the & to prefix subroutine calls when we introduce the idea of reusable code. This doesn’t sit well with some programmers because it’s not how the experienced programmers work. The & does some magic, which we don’t mention in the book, and it’s a bit crufty for the Perl 5 programmer. Continue reading “Why we teach the subroutine ampersand”

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.