Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Overloaded operators calling similar functions...

by Athanasius (Archbishop)
on Dec 08, 2012 at 02:56 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1007842]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-25 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found