By jove, I think I got it...

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.

$guess =~ /[$good_guesses]/
gets translates to ...
$guess =~ /[]/
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.

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...

elsif ((defined($good_guesses))&&($guess =~ /[$good_guesses]/))
that prevents the interpretation
$guess =~ /[]/
when $good_guesses is not defined

In reply to Re: Resetting variables by Sandy
in thread Resetting variables by yacoubean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.