in reply to Bad Code?
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
|
|---|