Commits

Shlomi Fish committed e349f8d

Bad elems: Regex: using $&, etc.

Comments (0)

Files changed (2)

             private ones).
         - Non-explicit imports.
             - hard to track where identifiers come from.
-        - 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.
       * Captures are ( ... )
       * Clusters are (?: ... )
       @ Added [Mon 2013-05-06]
+    - Bad elems: Regex: using $&, etc.
+        @ DONE [Tue 2013-05-28]
+      * Slows down the captures.
+      * use English without -no_match vars.
+      @ Added [Tue 2013-05-21]
 
 # vim:ft=quicktask:sw=4:ts=4:et

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

 </p>
 
 </item>
+<item xml:id="non_recommended_regex_vars">
+<info>
+<title>Non-Recommended Regular Expression-related Variables</title>
+</info>
+<p>
+The regular expression variables <code>$&amp;</code> (or
+<code>$MATCH</code> in <code>use English;</code>), <code>$`</code> (
+<code>$PREMATCH</code>), and <code>$'</code> (<code>$POSTMATCH</code>)
+should normally not be used because using them incurs a penalty on
+every regular expression match. As an alternative, one can use:
+</p>
+
+<code_blk syntax="perl">
+$` ⇒ substr($var, 0, $-[0])
+$&amp; ⇒ substr($var, $-[0], $+[0] - $-[0])
+$' ⇒ substr($var, $+[0])
+</code_blk>
+
+<p>
+Or alternatively use <code>${^MATCH}</code>,
+<code>${^PREMATCH}</code>, and <code>${^POSTMATCH}</code> from perl-5.10.0
+and onwards (see <pdoc d="perlvar" /> for more information about those).
+</p>
+
+<p>
+Furthermore a plain <code>use English;</code> statement should be replaced
+with a <code>use English qw( -no_match_vars ) ;</code> statement
+to avoid referencing these variables.
+</p>
+
+</item>
 </section>
 <section xml:id="sources_of_advice">
 <info>