<?xml version="1.0" encoding="windows-1252"?>
<node id="1007842" title="Re: Overloaded operators calling similar functions..." created="2012-12-07 21:56:08" updated="2012-12-07 21:56:08">
<type id="11">
note</type>
<author id="968231">
Athanasius</author>
<data>
<field name="doctext">
&lt;p&gt;This is a nicely-written program, well done! If the C++ version is up to this standard, you should get high marks on the assignment.&lt;/p&gt;

&lt;p&gt;I did notice one logical error:&lt;/p&gt;

&lt;code&gt;
    print "Division by zero not allowed, starting over..." and redo
      if ( $frac01 =~ /^[\s\S]?\/?0$/ );
&lt;/code&gt;

&lt;p&gt;If the user enters &lt;tt&gt;1/0&lt;/tt&gt;, this correctly flags division by zero. But,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the user enters &lt;tt&gt;12/0&lt;/tt&gt;, the regex fails and division by zero is not caught.&lt;/li&gt;
&lt;li&gt;If the user enters &lt;tt&gt;10&lt;/tt&gt;, the regex matches and gives a false positive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How to fix this? One way would be to extract the denominator (if any) and test whether that is zero. But this is Perl, so TMTOWTDI.&lt;/p&gt;

&lt;p&gt;Now to style: In Perl, it&amp;rsquo;s better to declare a variable as late as possible in the code. So, looking at this line:&lt;/p&gt;

&lt;code&gt;
my ( $f1, $f2, $frac01, $frac02, $command, $total ) = (0) x 6;
&lt;/code&gt;

&lt;p&gt;actually only &lt;tt&gt;$command&lt;/tt&gt; needs to be declared before the outer &lt;tt&gt;while&lt;/tt&gt; loop. The rest can safely be declared at the point of first use. (And yes, I would declare &lt;tt&gt;$total&lt;/tt&gt; separately within each of the &lt;tt&gt;if / elsif&lt;/tt&gt; blocks.)&lt;/p&gt;

&lt;p&gt;Hope that helps,&lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-968231"&gt;
&lt;p&gt;
&lt;table width="100%"&gt;
&lt;tr&gt;
  &lt;td align="left"&gt;
    Athanasius&amp;emsp;&lt;font color="#008000"&gt;&amp;lt;&lt;/font&gt;[href://http://www.biblegateway.com/passage/?search=John%203:16&amp;version=NLV|&lt;font color="#008000"&gt;&amp;deg;&lt;/font&gt;]&lt;font color="#008000"&gt;(((&amp;gt;&amp;lt;&lt;/font&gt;&amp;emsp;&lt;i&gt;contra mundum&lt;/i&gt;
  &lt;/td&gt;
  &lt;td align="right"&gt;
    [href://http://translate.google.com.au/#la/en/Iustus%20alius%20egestas%20vitae%2C%20eros%20Piratica%2C|&lt;b&gt;Iustus alius egestas vitae, eros Piratica,&lt;/b&gt;]
  &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007841</field>
<field name="parent_node">
1007841</field>
</data>
</node>
