Simple Patterns

In Perl, patterns are enclosed inside a pattern match operator, which is sometimes represented as m//. A simple pattern might appear as follows:

m/Simon/

The preceding pattern matches the letters S-i-m-o-n in sequence. But where is it looking for Simon? Previously, you learned that the Perl variable $_ is frequently used when Perl needs a default value. Pattern matches occur against $_ unless you tell Perl otherwise (which you'll learn about later). So the preceding pattern looks for S-i-m-o-n in the scalar variable $_.

If the pattern specified by m// is found anywhere in the variable $_, the match operator returns true. Thus, the normal place to see pattern matches is in a conditional expression, as shown here:

 if (m/Piglet/) ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD 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.