Commits

shl...@13c2ddb8-3aa7-4284-800b-8db07c650eef  committed 8c3796d

Extract a <bad_code> macro.

  • Participants
  • Parent commits a91f230

Comments (0)

Files changed (1)

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

 {#MAIN_LIST#:%body:##}
 </define-tag>
 
+<define-tag bad_code endtag="required">
+<pre class="bad_code">
+\# Bad code:
+
+%body
+</pre>
+</define-tag>
+
 {#MAIN_LIST#}
 
 <define-tag item endtag="required">
 reserved for pragmas. So this is bad:
 </p>
 
-<pre>
-\# Bad code!
+<bad_code>
 \# This is file person.pm
 package person;
 
 use strict;
 use warnings;
 1;
-</pre>
+</bad_code>
 
 <p>
 And this would be better:
 of old code and tutorials and is more prone to errors:
 </p>
 
-<pre>
-\# Bad code!
+<bad_code>
 my $new_object = new MyClass @params;
-</pre>
+</bad_code>
 
 <p>
 Instead use the <tt>MyClass-&gt;new(...)</tt> notation:
 references some programmers are tempted to access them directly: 
 </p>
 
-<pre>
-\# Bad code.
+<bad_code>
 $self-&gt;{'name'} = "John";
 print "I am ", $self-&gt;{'age'}, " years old\n";
 
 \# Or even (Really bad code:)
 $self-&gt;[NAME()] = "John";
-</pre>
+</bad_code>
 
 <p>
 However, this is sub-optimal as explained in
 For example, there is one in this code to shuffle a deck of cards:
 </p>
 
-<pre>
-\# Bad code.
+<bad_code>
 for my $i (0 .. 51)
 {
     my $j = $i + int(rand(52-$i));
     @cards[$i,$j] = @cards[$j,$i];
 }
-</pre>
+</bad_code>
 
 <p>
 This code is bad because the meaning of 52 and 51 is not explained and they
 One can sometimes see people write code like that:
 </p>
 
-<pre>
-\# Bad code.
+<bad_code>
 my $name = shift(@ARGV);
 
 print "$name", \n";
 {
     print "Your name begins with the letter 't'";
 }
-</pre>
+</bad_code>
 
 <p>
 However, it's not necessary to enclose $name in double quotes (i.e: 
 with:
 </p>
 
-<pre>
-\# Bad code:
-
+<bad_code>
 my ($a, $b) = @ARGV;
 .
 .
 .
 \# Won't work now.
 my @array = sort { length($a) &lt;=&gt; length($b) } @other_array; 
-</pre>
+</bad_code>
 
 <p>
 Instead, use other single-letter variable names such as 
 the length of the array:
 </p>
 
-<pre>
-\# Bad code:
+<bad_code>
 my @flags = ((0) x ($#names +1))
-</pre>
+</bad_code>
 
 <p>
 However this is unnecessary because one can better do it by evaluating
 One can often see people interpolate strings directly into regular expressions:
 </p>
 
-<pre>
-\# Bad code:
-
+<bad_code>
 my $username = shift(@ARGV);
 
 open my $pass_fh, '&lt;', '/etc/passswd'
     }
 }
 close($pass_fh);
-</pre>
+</bad_code>
 
 <p>
 The problem is that when a string is interpolated into a regular expression
 for executing commands for their side-effects. E.g:
 </p>
 
-<pre>
-\# Bad code:
-
+<bad_code>
 use strict;
 use warnings;
 
 my $temp_file = "tempfile.txt";
 
 `rm -f $temp_file`;
-</pre>
+</bad_code>
 
 <p>
 However, this is not idiomatic because <tt>`...`</tt> and <tt>qx/.../</tt> are
 One can write code while reading from STDIN:
 </p>
 
-<pre>
-\# Bad code:
-
+<bad_code>
 use strict;
 use warnings;
 
     }
     print $line;
 }
-</pre>
+</bad_code>
 
 <p>
 However, it is usually better to use <tt>ARGV</tt> instead of <tt>STDIN</tt>
 Sometimes you can see class constructors such as:
 </p>
 
-<pre>
-\# Bad code:
-
+<bad_code>
 sub new
 {
     my $proto = shift;
     my $self  = {};
     ...
 }
-</pre>
+</bad_code>
 
 <p>
 The problem here is that this allows one to do