Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

RE: a question on style

by Ovid (Cardinal)
on Jun 12, 2000 at 20:30 UTC ( #17732=note: print w/replies, xml ) Need Help??


in reply to a question on style

Two comments:
  1. Leave off the else if you don't use it. Including unused code is usually begging for confusion later on.
  2. Are you sure your code above is correct? You have an assignment operator "=" instead of a logical operator like "==" or "eq".
The second point is significant (and if it was just a typo, I'll complete this thought for others because I've tripped on this a time or two).

Consider the following code:

$x = 3; $y = "Ovid"; if ($x = $y) { print "This is true\n"; } else { print "This is false\n"; }
This code will always print "This is true" because the "=" assigns the value of $y to $x (in this case, both wind up with the value of "Ovid"). The if statement winds up evaluating the true/false value of "Ovid" and concludes because it is not undef and not zero, it must be true and the statement prints. Therefore, the above code probably doesn't behave the way you want it to.

Interestingly, the following snippet will work the way you expect it, but not for the reasons that are immediately obvious:

$x = 3; $y = 0; if ($x = $y) { print "This is true\n"; } else { print "This is false\n"; }
This code will print "This is false", but not because $x failed to equal $y. In this case, $y is assigned to $x and the if statement evaluated the resulting value, in this case '0' (zero) and zero evaluates as false.

Always remember when using an if statement or other conditional that you usually want to test equality with a logical operator such as "==" for numerics and "eq" for non-numerics.

Replies are listed 'Best First'.
RE: RE: a question on style
by jrsmith (Pilgrim) on Jun 12, 2000 at 20:36 UTC
    thanx, i know the difference between = and == that code is just a quick sample to illustrate my point, it serves no functionality
      After I got through about half of my reply, I thought to check your home node and realized that you had more XP than I and I suspected that I was teaching my grandpa how to suck eggs (you can just ignore that comment if you ain't from the South). I almost scratched the post, but figured that I have tripped so many times on that little problem that I figured it might be worth rehashing to others. Sorry if it was a bit pedantic.

      Hey, what do other monks think? Was I explaining something that was too obvious, like "here's how to add two numbers together"? I wouldn't mind getting a feel for that. I don't want to waste bandwidth if everyone feels they are way beyond that.

        I think you were fine in replying. The wording of the question would *seem* to indicate someone of not great experience, along with the comments on the home page. I was, in fact, thinking of a reply along a similiar vein before I went off to lunch. I couldn't think of a good example of the top of my head that would illustrate the point well. Yours was quite good (worthy of being ++'ed).

        And, unlike you, I didn't take the time to go checkout the homepage to see the XP/writeups (well, until you mentioned in your previous post, anyway).

        --Chris
        well truth be told you were correct in your assumption, i am still a relative newbie and i wasn't trying to flame your answer (i appreciated it :) i just wanted it known that i am not THAT much of a newbie.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-08-09 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?