8.2. A Menagerie of Typos

At this point, we're going to go on a tour of typo examples with different diagnoses.

8.2.1. Quotable Quotes

Our first program sets up a hash %quotation indexed by author and then goes on:

while (my ($author, $quote) = each %quotation)
   {
   print 'Quote of the Day: "$quote" by $author\n';
   }

This runs, but takes us too literally:

Quote of the Day: "$quote" by $author\nQuote of the Day:
"$quote" by $author\nQuote of the Day: "$quote" by $author\n...

Aha! Nothing is being interpolated because we're using single quotes. We change them to double quotes:

3  while (my ($author, $quote) = each %quotation)
4     {
5     print "Quote of the Day: "$quote" by $author\n";
6     }

Yikes, now we get three errors:

 Scalar found where operator ...

Get Perl Debugged now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.