This is the Perl 6 solution to the exercise which is the beginning of a game called the Number Guessing game.
examples/number_guessing.pl
use v6; my $hidden = (1..200).pick; # say $hidden; my $guess = prompt "Type in your guess: "; # say $guess; if $guess == $hidden { say "Hit!"; } elsif $guess < $hidden { say "Your guess is too small"; } else { say "Your guess is too big"; }
Perl 6 has a function called rand to generate random numbers, but as I was told, the idiomatic way to generate random integers is to pick them from a range or to roll a dice.
Once we have the $hidden value we use the prompt function to ask for the user input.
Unlike in Ruby or in Python Perl does not require special conversion of the input to a number. We can go directly to the look at the 3 cases and provide the appropriate feedback to the user.
Published on 2015-10-18