Using Patterns to Match Digits

Yesterday I introduced you to the bare basics of pattern matching. You learned how to look for strings contained inside other strings, which gives you some flexibility in your scripts and what sort of input you can accept and test. Today, and in future days, you'll learn about new kinds of patterns you can use in the pattern operator /.../ and how to use those to make your scripts more flexible and more powerful.

Today we'll look at patterns that are used to match digits, any digit, from 0 to 9. Using what you learned yesterday you could test for digits like this:

if ($input =~ /1/ or $input =~ /2/ or $input =~ /3/ or $input =~ /4/ or
   $input =~ /5/ or $input =~ /6/ or ... }

But that would be a lot of repetitive ...

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.