in reply to Resetting variables
Firstly, although I am not certain, I believe the error message is erroneously pointing to the wrong line $guess !~ m/[[:alpha:]]/ rather the error occurs in the elsif portion, where you have
$guess =~ /[$good_guesses]/
During the 1st pass, $good_guesses is either undefined, or set to a single letter (or multiple letter? doesn't matter).
On second pass, the $good_guesses is reset to a null string.
gets translates to ...$guess =~ /[$good_guesses]/
However, if I am not mistaken, character classes will accept a ']' as a valid element of the character class, if it is the first element of the class. So, the parser is taking the ']' as an element of the class, and then continues to look for the closing square bracket.$guess =~ /[]/
For whatever reason, if $good_guesses is undefined, the parser figures it out the way you meant. (UPDATE: Nope, that's not the reason, see update below)
To get your code to work, I re-initialized $good_guesses = undef; in your "reset_game" sub.
PS: I wrote my response after I got your program to work, but I did not actually go and read up on all the particulars of character classes in regular expressions, so I may not be entirely accurate.
Sandy
PS (again) ... That was fun!
UPDATE: Made mistake... the reason the code works when $good_guesses is undefined is that there is a condition in front of the condition...
that prevents the interpretationelsif ((defined($good_guesses))&&($guess =~ /[$good_guesses]/))
when $good_guesses is not defined$guess =~ /[]/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 21:36 UTC | |
by Sandy (Curate) on Dec 21, 2004 at 22:06 UTC | |
|
Re^2: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 22:04 UTC | |
by Sandy (Curate) on Dec 21, 2004 at 22:09 UTC | |
by yacoubean (Scribe) on Dec 21, 2004 at 22:17 UTC |