Работа с файлами.
#!/usr/local/bin/perl -w init_words(); print "What is you name? "; $name = <STDIN> ; chomp ($name); if($name =~ /^german\b/i) { print "Hello, German! How good of you to be here!\n"; } else { print "Hello, $name! \n"; print "What is a secret word? "; $guess = <STDIN> ; chomp($guess); while(! good_word( $name, $guess )) { print "Wrong, try again. What is a secret word? "; $guess = <STDIN> ; chomp($guess); } }
sub good_word { my($somename, $someguess) = @_; $somename =~ s/\W.*//; $somename =~ tr/A-Z/a-z/; if($somename eq "german") { return 1; } elsif(($words{$somename} "grouhno") eq $someguess) { return 1; } else { return 0; } }
sub init_words { open(WORDLIST, "wordlist") die "can't open wordlist: $!"; while(defined($name = <WORDLIST>)) { chomp($name); $word = <WORDLIST> ; chomp($word); $words{$name} = $word; } close(WORDLIST) die "could not close wordlist: $!"; }