An Example: Simple Statistics
Here's an example called stats.pl, which prompts you for numbers, one at a time. When you're done entering numbers, it gives you a count of the numbers, the sum, and the average. It's a rather silly kind of statistics script, but it'll demonstrate tests, variable assignment, and pattern matching for input verification (and we'll be building on this script later on). Here's an example of what it looks like when run (including what happened when I accidentally typed an r in the middle of entering the numbers):
% stats.pl Enter a number: 3 Enter a number: 9 Enter a number: 3 Enter a number: r Digits only, please. Enter a number: 7 Enter a number: 4 Enter a number: 7 Enter a number: 3 Enter a number: Total count of ...
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.