Commits

Shlomi Fish committed d49a333

Add the clustering instead of capturing.

Comments (0)

Files changed (2)

             private ones).
         - Non-explicit imports.
             - hard to track where identifiers come from.
-        - Regex: capturing groups instead of clustering ones
-          * Captures are ( ... )
-          * Clusters are (?: ... )
-          @ Added [Mon 2013-05-06]
+        - Regex: using $&, etc.
+          * Slows down the captures.
+          * use English without -no_match vars.
+          @ Added [Tue 2013-05-21]
     - Add a page about file/directory-handling :
         - Will share a lot of content with the Sys Admin page.
         - Refer to perldoc -f open and perldoc perlopentut.
         @ DONE [Fri 2013-05-03]
     - Bad elems: Generating Invalid markup (HTML, etc.)
         @ DONE [Sat 2013-05-04]
+    - Bad elems: Regex: capturing groups instead of clustering ones
+        @ DONE [Tue 2013-05-21]
+      * Captures are ( ... )
+      * Clusters are (?: ... )
+      @ Added [Mon 2013-05-06]
 
 # vim:ft=quicktask:sw=4:ts=4:et

src/tutorials/bad-elements/perl-elements-to-avoid.xml-grammar-vered.xml

 EOF
 </code_blk>
 </item>
+<item xml:id="capturing_instead_of_clustering_in_regexes">
+<info>
+<title>Capturing Instead of Clustering in Regular Expressions</title>
+</info>
+<p>
+If you want to group a certain sub-expression in a
+<a xlink:href="$(ROOT)/topics/regular-expressions/">regular expression</a>,
+without the need to capture it (into the <code>$1</code>, <code>$2</code>,
+<code>$3</code>, etc. variables and related capture variables), then
+you should cluster them using <code>(?: … )</code> instead of capturing
+them using a plain <code>( … )</code>, or alternatively not grouping them
+at all if it's needed. That is because using a cluster is faster and cleaner
+and better conveys your intentions than using a capture.
+</p>
+
+<bad_code syntax="perl">
+if (my (undef, $match) = ($str =~ /\A(BA+)*\[([^\]]+)\]/))
+{
+    print "Found $match\n";
+}
+</bad_code>
+
+<p>
+A better code would be:
+</p>
+
+<code_blk syntax="perl">
+if (my ($match) = ($str =~ /\A(?:BA+)*\[([^\]]+)\]/))
+{
+    print "Found $match\n";
+}
+</code_blk>
+
+<p>
+Note that if you can afford to run your code only on perl 5.10.x and above,
+then you can use <a xlink:href="http://www.perlmonks.org/?node_id=680796">named
+captures</a>.
+</p>
+
+</item>
 </section>
 <section xml:id="sources_of_advice">
 <info>