Teaching Perl - Week 2 ( part2 )
This part2 was prompted by Chromatic’s post on state. I’d never heard of state before, and it’s documentation is poor. Let’s take a look at how state could affect our game.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #!/usr/bin/env perl # guess a number game use strict; use warnings; use feature qw( say state switch ); say 'welcome'; # generate the winning number between 1 and 10 # see perlfaq4 for algorithm details my $winning_num = 1 + int( rand( (10-1)+1 ) ); until ( $winning_num == state $guess ) { say 'Guess a number between 1 and 10: '; $guess = readline(*STDIN); # check to see if we have a winner, or the guess is to high, or low.