Like Abigail and others say, there are ways to improve your code. But as far as being different from Randall's textbook codee, there's nothing wrong. After all, your code works correctly, right?

Bonus points for using unless. People are often intimidated by unless or just don't see the opportunity to use it appropriately.

When you finish a chunk of code, it's worth looking at it to see to see what is clumsy, what can be simplified. In this case, for example, you order the contents of the loop so that detecting 'quit' and exiting the loop happens at the bottom ... because you started with until ($guess eq $secret_number ) { . If the user plays to completion, the exit happens at the top, but if they give it, the loop is exited due to last. Neither is better or more natural than the other, so it's worth considering rotating the loop to natural order: prompt, input, test input, test input, output message.

until ($guess eq $secret_number) { print "\nEnter a number between 1 and 100 : "; chomp($guess = <STDIN>); if ($guess =~ /quit|exit|^\s*$/i) { $quit = "yes"; last } my $answer = ($guess < $secret_number) ? "less than" : ($guess > $secret_number) ? "greater than": ""; print "\nThe number you entered is $answer the secret number.\n"; }

Yes, you exit in the middle of the loop, nothing wrong with that. In some languages exiting a loop is tricky, but it's easy in Perl. You were already using last, anyway.

The next complicated structure is the regex and exit. Look at the artificial variable, $quit. Instead of using the variable, you could move the printed message into the regex condition.

Hmm .... it's beginning to look like Abigail's solution.

Your code works. Once you have code that works, Then you can make it work better, do more, more effectively communicate what you consider important.

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: Bad Code? by TomDLux
in thread Bad Code? by Hayl_

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.