Lesson 22
Perl Features Web Programming
Perl features as they apply to Web Programming
In this module you learned some of the more useful features of the Perl language as they apply to Web programming.
You learned about regular expressions, arrays, hashes, references, and how to use multidimensional and complex data structures.
Assume you have a list of strings and want to print all strings containing the letters cat because, like your author, you love cats.
my @words = (
'laphroaig',
'house cat',
'catastrophe',
'cat',
'is awesome',
);
foreach my $word (@words) {
if ( $word =~ /cat/ ) {
print '$word\n';
}
}
That prints out:
house cat
catastrophe
cat
The basic syntax of a regular expression match looks like this:
STRING =~ REGEX
The =~ is known as a binding operator. By default, regular expressions match against the built in $_ variable, but the binding operator binds it to a different string. So you could write the loop like this:
foreach (@words) {
if (/cat/) {
print '$_\n';
}
}
There is also a negated form of the binding operator, !~ used to identify strings not matching a
given regular expression:
foreach my $word (@words) {
if ( $word !~ /cat/ ) {
print '$word\n';
}
}