Secure Developments

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. #!/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. Read more →