I have a simple game I'm writing to expand my perl skills (I'm still a noob). The script runs in a while loop, and calls a bunch of subs. I want to give the player the option to play again after a game is finished, but still keep some of my data stored in variables. However, the problem I'm having is resetting some variables back to 0 or "". The first run through, everything works fine. But after selecting play again, I start getting errors with my variables. Heres some code:
while ($word_len >= $tot_found) {
if ($word_len == $tot_found) {
print_word();
play_again();
} else {
print_word();
if ($guessed_word == 0) {
get_guess();
check_guess();
} else {
play_again();
}
}
}
sub get_guess {
$guess = <stdin>;
chomp($guess);
}
sub check_guess {
my $occurrences = "";
my $guess_len = length($guess); print "\n$guess\n";
if ($guess !~ /[[:alpha:]]/) {
print "\n***Invalid letter*** (hit any key)\n"; <>;
}
# other stuff...
}
sub play_again {
print "Do you want to play again (y or n)? ";
my $play_again = <STDIN>;
chomp($play_again);
if ($play_again eq "y") {
reset_game();
} else {
last;
}
}
sub reset_game {
($good_guesses, $bad_guesses) = "";
($tot_found, $found_one, $occurrences, $all_occurrences, $guessed_
+word) = 0;
}
On the second run through, I'm getting this error on the
if ($guess !~ /[[:alpha:]]/) { line:
Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ]/ at...
The variable
$guess gets reset everytime a guess is made, so I didn't think I needed to reset it in the
reset_game sub. But I tried that anyway and I still get the error.
I'm not sure if I'm using the best approach here, so any ideas would be much appreciated. TIA.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.