[svn:perlfaq] r10468 - perlfaq/trunk

[email protected] Tue, 1 Jan 2008 12:27:25 -0800 (PST)
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
Author: comdog
Date: Tue Jan  1 12:27:24 2008
New Revision: 10468

Modified:
   perlfaq/trunk/perlfaq6.pod

Log:
* perlfaq6: How do I efficiently match many regular expressions at once?
	+ The second code example was supposed to be like the first
	+ used the C<> around things that should have had it before


Modified: perlfaq/trunk/perlfaq6.pod
==============================================================================
--- perlfaq/trunk/perlfaq6.pod	(original)
+++ perlfaq/trunk/perlfaq6.pod	Tue Jan  1 12:27:24 2008
@@ -574,8 +574,8 @@
 ( contributed by brian d foy )
 
 Avoid asking Perl to compile a regular expression every time
-you want to match it.  In this example, perl must recompile
-the regular expression for every iteration of the foreach()
+you want to match it. In this example, perl must recompile
+the regular expression for every iteration of the C<foreach>
 loop since it has no way to know what $pattern will be.
 
 	@patterns = qw( foo bar baz );
@@ -592,11 +592,11 @@
 			}
 		}
 
-The qr// operator showed up in perl 5.005.  It compiles a
+The C<qr//> operator showed up in perl 5.005.  It compiles a
 regular expression, but doesn't apply it.  When you use the
 pre-compiled version of the regex, perl does less work. In
-this example, I inserted a map() to turn each pattern into
-its pre-compiled form.  The rest of the script is the same,
+this example, I inserted a C<map> to turn each pattern into
+its pre-compiled form. The rest of the script is the same,
 but faster.
 
 	@patterns = map { qr/\b$_\b/i } qw( foo bar baz );
@@ -605,13 +605,16 @@
 		{
 		foreach $pattern ( @patterns )
 			{
-			print if /$pattern/i;
-			next LINE;
+			if( /$pattern/ )
+				{
+				print;
+				next LINE;
+				}
 			}
 		}
 
 In some cases, you may be able to make several patterns into
-a single regular expression.  Beware of situations that require
+a single regular expression. Beware of situations that require
 backtracking though.
 
 	$regex = join '|', qw( foo bar baz );
@@ -621,8 +624,8 @@
 		print if /\b(?:$regex)\b/i;
 		}
 
-For more details on regular expression efficiency, see Mastering
-Regular Expressions by Jeffrey Freidl.  He explains how regular
+For more details on regular expression efficiency, see I<Mastering
+Regular Expressions> by Jeffrey Freidl.  He explains how regular
 expressions engine work and why some patterns are surprisingly
 inefficient.  Once you understand how perl applies regular
 expressions, you can tune them for individual situations.