Blob


1 #!/usr/bin/perl
3 # Modify the program from the previous exercise to print extra
4 # debugging information as it goes along, such as the secret number
5 # it chose. Make your change such that you can turn it off, but
6 # yoru program emits no warnings if you turn it off. If you are
7 # using Perl 5.10 or later, user the // operator. Otherwise, use
8 # the conditional operator.
10 use v5.10;
11 use warnings;
12 use strict;
13 use utf8;
15 my $secret = int(1 + rand 100);
16 my $debug = $ENV{DEBUG} // 1;
17 my $guess;
18 print "Guess (1-100): ";
19 while (chomp($guess = <>) && $guess !~ /(exit|quit)|\A\z/) {
20 print "Secret: $secret\n" if $debug;
21 if ($guess == $secret) {
22 print "Correct!\n";
23 last;
24 } elsif (defined($guess) && $guess < $secret) {
25 print "Too low!\n";
26 } elsif (defined($guess) && $guess > $secret) {
27 print "Too high!\n";
28 }
29 print "Guess (1-100): ";
30 }
31 print "Exiting!\n";