#!/usr/bin/perl
&init_words;
print "What is your name? ";
$name=<STDIN>;
chop($name);
if ($name =~ /^randal\b/i) {print "Hello, Randal! How good... \n";}
else 
{ 
	print "Hello, $name!\n";
	print "What is the secret word? ";
	$guess=<STDIN>;
	chop($guess);
	while (! &good_word($name,$guess))
	{
		print "Wrong, try again... ";
		$guess=<STDIN>;
		chop($guess);
	}
}

sub init_words
{
	while ($filename=<*.secret>)
	{
		open(WORDSLIST,$filename);
		if (-M WORDSLIST < 7)
		{
			while ($name=<WORDSLIST>)
			{
				chop($name);
				$word=<WORDSLIST>;
				chop($word);
				$words{$name}=$word;
			}
		}
		close(WORDSLIST);
	}
}

sub good_word
{
	local($somename,$someguess)=@_;
	$name =~ s/\W.*//;
	$name =~ tr/A-Z/a-z/;
	if ($somename eq "randal") {1;}
	elsif (($words{$somename} || "groucho") eq $someguess) {1;}
	else 
	{
		open(MAIL,"|mail jclevin\@nova.edu");
		print MAIL "bad news: $somename guessed $someguess\n";
		close(MAIL);
		0;
	}
}