Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to RE: a question on style by Ovid
in thread a question on style by jrsmith

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2018-07-16 22:44 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (352 votes). Check out past polls.