#!/usr/bin/perl use Modern::Perl; # This is a guessing game in Perl. my $target= (int rand 100) + 1; my $count= 1; say "Welcome to the Perl Whole Number Guessing Game!"; say "Please enter a number between 1 and 100 and I will tell you if the number"; say "you're trying to guess is higher or lower than your guess.\n"; my $guess= -1; my @guesses; while ($guess != $target) { if ($count > 1) { $" = ", "; say "\nYour guesses so far: @guesses"; } print "Enter guess #$count: "; $guess= <>; chomp $guess; if ($guess < 1 || $guess > 100) { say "Your guess of $guess is out of range and will not be recorded. Try again. "; } elsif ($guess > $target) { say "Your guess of $guess is too high. Try lower next time. "; $count++; push @guesses, $guess; } elsif ($guess < $target) { say "Your guess of $guess is too low. Try higher next time. "; $count++; push @guesses, $guess; } else { say "\nYou guessed the secret number ($target) in $count tries! "; $" = ", "; say "Here were your guesses: @guesses, and $guess\n"; } }