Firstly, many thanks for the excellent book -- the precision and clarity of the explanations is outstanding, and the practical value of starting off with something that is actually useful like faqgrep (instead of endless noddy programs like some books) is tremendous. OK, my first question:

In discussing the mathq program in chapter 3, on pages 35 and 48 you give two examples of the output when an incorrect answer is entered, like this:

Incorrect: 21 / 7 = 3 # p35 Incorrect: 0 / 0 = 8 # p48

In fact, since the program as given on p44-45 is just printing the original question, a space and then the solution, it prints this:

Incorrect: 21 / 7 = ? 3 Incorrect: 0 / 0 = ? 8

So we need to chop off the end of the question and replace it with the right answer. Here's my solution, using substr (top of p45):

which now prints the responses as shown on pp35 and 48. Given the nature of Perl, I am sure there are many other ways to do the same thing, using something other than substr. There's probably no "best" way, but what would be the most Perlish way? What would a veteran Perl programmer do here?

Hmm, quite surprising this discrepancy has gone unnoticed so long --- and the spec on p35 also shows the questions indented one space as well (and an exclamation point after 'Correct'). After this much time, I am inclined to not call it an errata but rather an undocumented exercise for the reader --- just as you seem to have done

As to your fix, it is simple, direct, and it works! Plus, although a regex substitution solution might seem more "perlish" (ie, swap the '?' with the $solution):

it is often good to stick with plain string functions like substr() when they are all that's required!

Two other alternatives also present themselves: 1) go back and rewrite the specs and example output while no one is looking so the current code is actually correct --- too late for that I guess, or 2) fix the problem closer to the source: ie, change the $question string so it does not have the ' ?' in it at all (making sure we change both places where the $question string is defined), and put the ' ?' in the print statement that displays the question:

print "$question ? ";

That actually requires changes to 3 existing lines of code (rather than the addition of new code) --- but these changes are tiny (delete a couple characters from two lines, add a couple characters to another line), and the problem disappears instead of needing fixing.

What's the "best" way? Of the two "fixit" methods, I'd guess a number of experienced Perl programmers would immediately think "regex" and try some variation on the substitution method.

Personally, I think your substr() method is simpler and more efficient (even if you hadn't said all those nice things about the book). And if you modify the $question variable itself rather than creating a new variable, it doesn't require any change to the original print statement that followed:

Yes, your regex solution is exactly the sort of thing I was thinking of when I said a more "Perlish" (perlish?) way of doing it -- it's short, to the point and has the requisite cryptic punctuation! And in the substr version, of course you are right about simply modifying the $question variable instead of unnecessarily creating a new one. It's kinda hard trying to remember Perl syntax *and* Occam's razor at the same time