Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Overloaded operators calling similar functions...

by Athanasius (Chancellor)
on Dec 08, 2012 at 02:56 UTC ( #1007842=note: print w/replies, xml ) Need Help??

in reply to Overloaded operators calling similar functions...

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.

I did notice one logical error:

print "Division by zero not allowed, starting over..." and redo if ( $frac01 =~ /^[\s\S]?\/?0$/ );

If the user enters 1/0, this correctly flags division by zero. But,

  • If the user enters 12/0, the regex fails and division by zero is not caught.
  • If the user enters 10, the regex matches and gives a false positive.

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.

Now to style: In Perl, it’s better to declare a variable as late as possible in the code. So, looking at this line:

my ( $f1, $f2, $frac01, $frac02, $command, $total ) = (0) x 6;

actually only $command needs to be declared before the outer while loop. The rest can safely be declared at the point of first use. (And yes, I would declare $total separately within each of the if / elsif blocks.)

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007842]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-06-25 13:05 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.