#!/usr/bin/perl #ex_hello # Learning Perl, Chapter 1, "say hello" program &init_words; @password = getpwuid($<); # get the password data $name = $password[6]; # get just the GCOS field $name =~ s/,.*//; # throw away everything after first comma if ($name =~ /^randal\b/i) { # back to the other way :-) print "Hello, Randal! How good of you to be here!\n"; } else { print "Hello, $name!\n"; # ordinary greeting print "What is the secret word? "; $guess = ; chop($guess); while (! &good_word($name,$guess)) { print "Wrong, try again. What is the secret word? "; $guess = ; chop($guess); } } dbmopen(%last_good,"lastdb",0666); $last_good{$name} = time; dbmclose(%last_good); sub init_words { while ($filename = <*.secret>) { open(WORDSLIST, $filename); if (-M WORDSLIST < 7) { while ($name = ) { chop($name); $word = ; chop($word); $words{$name} = $word; } } else { # rename the file so it gets noticed rename($filename,"$filename.old"); } close(WORDSLIST); } } sub good_word { local($somename,$someguess) = @_; # name the parameters $somename =~ s/\W.*//; # delete everything after first word $somename =~ tr/A-Z/a-z/; # lowercase everything if ($somename eq "randal") { # should not need to guess 1; # return value is true } elsif (($words{$somename} || "groucho") eq $someguess) { 1; # return value is true } else { open(MAIL,"|mail YOUR_ADDRESS_HERE"); print MAIL "bad news: $somename guessed $someguess\n"; close(MAIL); 0; # return value is false } }