Mike Gossland’s Perl Tutorial Course for Windows

Chapter 2: Matching and Substitution in Perl

IntroductionBinding Operator > Regular Expressions >
Examples > Substitution > Advanced Ideas

Introduction to Matching

The ease and power of Perl's pattern matching is one its true strengths and a big reason why Perl is as popular as it is. Almost every script you write in Perl will have some kind of pattern matching operation because so often you want to seek something out, and then take an action when you find it.

Matching and substitution are very important because this is how you do editing "on the fly". This is how you create content customized to your Web visitor. You need to be able to open HTML templates and swap in information pertaining to your visitor. Matching and then substituting is just the way to do it.

Also in many other administrative tasks, such as searching through log files or web pages for particular words or sequences, pattern matching is the way to go. Take your web stats for example. All the web stats for your site's traffic are built from data recorded in the server log files. While not all stats programs are based on Perl, you can be sure that they are all looking through the log files for certain patterns of text, and this is the sort of thing that Perl excels at.

Pattern matching, in Perl at least, is the process of looking through sections of text for particular words, letters-within-words, character sequences, numbers, strings of numbers, html tags, what have you... and working with them.

In general, whatever you are seeking can be represented as a text pattern, whether it is a very explicit one, like looking for a specific word like "goodness", or a much more general one, like looking for a  North American phone number: three digits, three digits, then four digits.

These more complicated search expressions fall into the category of "regular expressions". This is an extremely important part of Perl and we will devote the rest of this class to this topic.

Next Page

Search this site or the web:

just this site the whole web

Questions? Feedback?

Email me your comments!