A good quick reference on the operators involved can be found at perlop. Breaking down your two expressions:

$z = $y ? $x / $y : 0;

  1. Given the ternary Conditional Operator, start with the conditional term.
  2. In Perl, 0 is equivalent to false for logical operators (as are nulls/undefs), so that term is equivalent to $y!=0
  3. If $y is not zero, the ternary returns $x/$y
  4. If $y is zero, the ternary returns 0;
  5. Lastly, $z gets the value returned by ? :

$z = $y && $x / $y;

  1. Given the binary C style Logical And, start with the first term.
  2. If $y == 0, the first term is false, so return false/0.
  3. If $y != 0, the first term is true so evaluate the second term.
  4. If $x / $y == 0, the second term is false, so return false/0.
  5. If $x / $y != 0, the second term is true, so return true.
  6. When && returns true, it returns the return value of the last expression in the chain, hence $x / $y.

Hope that's clear...


In reply to Re: Could someone please explain this to me in newbie terms :) by kennethk
in thread Could someone please explain this to me in newbie terms :) by raisputin

Title:
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, details, 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, summary, 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.