Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl: the Markov chain saw
 
PerlMonks  

Re^2:Example 3: expect the unexpected

by Mabooka-Mabooka (Sexton)
on Apr 17, 2005 at 16:43 UTC ( #448652=note: print w/ replies, xml ) Need Help??


in reply to Re: Common Beginner Mistakes
in thread Common Beginner Mistakes

One would believe it fails... it doesn't

It tries to do the right thing. The result in general is unpredictable. (That is, for a beginner).
The snippet is:
#! /usr/local/bin/perl use strict; sub print_percentage { my ($total, $part) = @_; if (int($total) != 0){ printf ("It's about %.2f%%\n", $part/$total *100.); } else{ print "Bad usage: $total == 0!!! Cannot divide by '$total'.\n" +; } } print_percentage(100, 50); print_percentage("oops", 50); ;1
One could write many variations on the theme:
#! /usr/local/bin/perl use strict; if ("38" == 38){ print "hit the jackpot!\n"; } else { print "hit elsewhere....\n"; } ;1

Why people do this?

People do mistakes, and they are spoiled by getting used to compilers' help. Many who come to Perl from other languages are used to "it doesn't make any sense" compiler's error. Or run-time exceptions. They are NOT used to the machine "doing the right thing" for them (of course! what do you mean by doing the right thing?) And that's the problem for beginners.

Solution

Set up your mind not to guess what it'll do. Learn, find out, know.


Comment on Re^2:Example 3: expect the unexpected
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-04-21 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls