A Number Speller

This second example isn't particularly useful for the real world, but does show some more complex uses of ifs, whiles, and various tests. This script asks you to enter a single-digit number (and has the usual verification tests to make sure you have indeed entered a single-digit number), and then it spells out that number for you—in other words, 2 is two, 5 is five, and so on. Then, it asks you if you want to enter another number, and if so, it repeats the process. Here's an example:

% numspeller.pl
Enter the number you want to spell: foo
No strings.  0 through 9 please..
Enter the number you want to spell: 45
Too big. 0 through 9 please.
Enter the number you want to spell: 6
Number 6 is six
Try another number (y/n)?: foo y or ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition 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.