in reply to Re^2: how to enable, disable strict
in thread how to enable, disable strict
There are many ways to achieve that. An easy way is to repeat the code that generates of the number in the success block. Here's a slightly different way to do it using a "special" value (undef) and using an if as a statement modifier:
#!/usr/bin/perl -w use warnings; use strict; my $secret; while (1) { $secret = int(1 + rand 100) if ! defined $secret; print "please enter a guess from 1 to 100: "; chomp (my $guess = <STDIN>); if ($guess =~ /quit|exit|^s*$/i) { print "Sorry you gave up. The number was $secret.\n"; last; } if ($guess < $secret) { print "You failed, try higher.\n"; } elsif ($guess > $secret) { print "You failed, try lower.\n"; } elsif ($guess == $secret) { print "You got it!\n"; $secret = undef; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: how to enable, disable strict
by elsiddik (Scribe) on Apr 25, 2007 at 10:58 UTC |