Well, I don't want to over-explain since the best way to learn is often to figure things out for yourself. However, let's examine what I suppose could be the 2 most confusing parts.

ask ($operands[0] > $operands[1], @operands);

This is a subroutine call. You'll see we've defined the subroutine ask in the script? This is how it is called. There are either 2 or 3 arguments inside the brackets depending on how you see things. The first one is $operands[0] > $operands[1] which is a logical expression and therefore either true or false and the second one is @operands which is an array with the 2 random values previously assigned. Note that originally this line had an @ symbol at the start of $operands[0] which although OK was misleading (and a typo) so I've corrected it now.

my $answer = $subtract ? $args[0] - $args[1] : $args[0] + $args[1];

This is a variable assignment which computes the answer in one way or another depending on the logical value in $subtract which we passed into the sub as the first argument. It uses the ternary operator which is of the form condition ? eval-if-true : eval-if-false. Think of it as a short-hand way of writing an if-else block.

The rest of the code is just loops and prints, really. See if you can trace through the logic now.


In reply to Re^5: Repeat question (redo) by hippo
in thread Repeat question by hchana

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.