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
';
   }

This runs, but takes us too literally:

Quote of the Day: "$quote" by $author
Quote of the Day:
"$quote" by $author
Quote of the Day: "$quote" by $author
...

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
";
6     }

Yikes, now we get three errors:

Scalar found where operator expected at typo.pl line 4, near
""Quote of the Day: "$quote"
        (Missing operator before $quote?)
syntax error at typo.pl line 4, near ""Quote of the Day: "$quote"
String found where operator expected at typo.pl line 4, near
"$quote" by $author
""
        (Missing operator before " by $author
"?)
Execution of typo.pl aborted due to compilation errors.

Once again, we consider only the first message. Whenever Perl provides a hint after the error, read it carefully. Perl suggests that we omitted an operator before $quote, but why would it expect an operator in the middle of a string? Aha—we must not be in a string any more.

At this point I would have pointed out that the syntax coloring feature of Emacs cperl mode would have shown this, but I promised Ed not to make fractious text editor advertisements.


At least we don't have to look far to find the problem; the string ended prematurely one character before $quote. There is, of course, more than one way to fix it. We could escape the " inside the string with backslashes, or we could use the spiffy qq operator, which allows us to pick any delimiter to use for a double-quoted string:

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

8.2.2. A Capital Typo

Our next horror feature is The Program That Wouldn't Die:

#!/usr/bin/perl
use Getopt::Std;

sub usage
  {
  print
     "Allowable options: -a, -c, -e, -n count, -u
";
  Exit;
  }

usage() unless getopts('acen:u'),
print "Starting program $0
";

Let's run it:

% typo.pl -q
Unknown option: q
Allowable options: -a, -c, -e, -n count, -u
Starting program typo.pl

Why did this program keep going even after it went into the usage error routine? For the answer, this is the last time we're going to tell you:

Put use strict and -w in all your programs!

Because when we do, we see:

Bareword "Exit" not allowed while "strict subs" in use at
typo.pl line 8.
Execution of typo.pl aborted due to compilation errors.

We should have used exit instead of Exit. (Perl built-in functions do not contain capital letters, and the reserved words that do are all uppercase.) Better yet, we should have called die instead of print.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset