Anonymous avatar Anonymous committed 39ccf3e

Add $a and $b to the bad elements.

Comments (0)

Files changed (4)

 
 * Page about Bad Elements:
     - Array element: @array[$idx] instead of $array[$idx].
+    - using $a and $b as variable names.
     - qx/`....` instead of system().
         - also open.
     - last/next/redo/etc. without explicit LABELs.
-    - using $a and $b as variable names.
     - comments and identifiers in a foreign language. 
     - STDIN instead of ARGV.
     - abuse of $#array (like $#array + 1 for array length).

src/tutorials/bad-elements/index.html.wml

 be used in list context.
 </p>
 
+<h3 id="vars-a-and-b">Variables called $a and $b</h3>
+
+<p>
+One should not create lexical variables called <tt>$a</tt> and <tt>$b</tt>
+because there are built-in-variables called that used for 
+<pdoc_f f="sort">sorting</pdoc_f> and other uses (such as reduce in
+<cpan_self_mod m="List::Util" />), which the lexical variables will interfere
+with:
+</p>
+
+<pre>
+\# Bad code:
+
+my ($a, $b) = @ARGV;
+.
+.
+.
+\# Won't work now.
+my @array = sort { length($a) &lt;=&gt; length($b) } @other_array; 
+</pre>
+
+<p>
+Instead, use other single-letter variable names such as 
+<tt>$x</tt> and <tt>$y</tt>, or better yet give more descriptive names.
+</p>
+
 </main_list>
 
 #include "bad-elements-sources.wml"
 <restore d />
 </define-tag>
 
+<define-tag pdoc_f whitespace="delete" endtag="required">
+<preserve f />
+<set-var %attributes />
+<a href="http://perldoc.perl.org/functions/<get-var f />.html">%body</a>
+<restore f />
+</define-tag>
+
 <define-tag nothing></define-tag>
 
 <perl_begin_news>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.