Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: My habitual errors

by ptum (Priest)
on Apr 04, 2007 at 17:12 UTC ( #608328=note: print w/ replies, xml ) Need Help??


in reply to My habitual errors

Even after all this time, I still sometimes inadvertently use assignment instead of testing for numeric equality:

if ($first = $second) { # do something } else { # this code is never executed since assignment to $first always retu +rns true }

Sigh. It is dumb, but I did it just last week.

Update: as pointed out by jdporter, in cases where $second evaluates as false, the resulting assignment to $first will also return false. While this rarely happens to me (since I'm usually trying to evaluate numerical equality between non-zero variables), it is an important distinction.


Comment on Re: My habitual errors
Download Code
Re^2: My habitual errors
by imp (Priest) on Apr 04, 2007 at 17:29 UTC
    For comparisons that have a constant I keep that value on the left hand side to avoid accidental assignment. Doesn't work for expressions that use two variables of course.
    if (0 == $n) { } elsif (1 == $n) { }
Re^2: My habitual errors
by jdporter (Canon) on Apr 04, 2007 at 17:33 UTC
    # this code is never executed since assignment to $first always returns true

    No; it will be false when $second is false.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-07-04 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls