An annoying error on page 783 of The Camel Book

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Programming Perl, 4th ed, PDF version, page 783 says that
$OUTPUT_FIELD_SEPARATOR is the same as $/ .
Wrong. It's actually $, rather than $/ .

That had me pulling my hair out for an hour, because a script
I just wrote was smashing all its output together:

#! /usr/bin/perl
#  make-all-list.perl
use v5.14;
use strict;
use warnings;
our @names = `ls -1`;
our @tomake;
foreach (@names)
    if (m/\.(c|cpp)$/)
       push(@tomake, $_);
$/ = ' ';
print(@tomake, "\n");

That's supposed to write a space-separated list of exe file names
corresponding to *.c and *.cpp files in the current directory,
assuming that each exe file has exactly one source file.
It gets run from inside a makefile like so:

ALL = $(shell make-all-list.perl)
all: $(ALL)
    @echo Fractals Program Collection is up to date.
(followed by rules for making exe files from c and cpp files)

Expected output:
hennon.exe julia.exe mandel.exe mandelbrot.exe sierpinski.exe spirograph.exe

Actual output:

It turns out, the output field separator variable is actually
"$," rather than "$/"; the program worked fine when I changed that one

This is already on O'Reilly's errata page, I see. But it's something
to keep in mind for anyone who wants to print uninterpolated arrays
and have spaces printed between elements.

Of course I *could* have done THIS:
print("@tomake", "\n");
which would insert spaces because $" defaults to ' '.

Or, I suppose I could have put the space in the s/// like so:
s/\.(c|cpp)$/.exe /;
Hmmm. Always multiple ways to do things in this language, it seems.

Robbie Hatley
Midway City, CA, USA
perl -le 'print "4o6e7o4f0w5llc7m"'

Site Timeline