Perl практика

         

Окончательный вариант программ.


Приветствие:

#!/usr/local/bin/perl -w init_words(); print "What is you name? "; $name = <STDIN&gt ; 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&gt ; chomp($guess); while(! good_word( $name, $guess )) { print "Wrong, try again. What is a secret word? "; $guess = <STDIN&gt ; chomp($guess); } } dbmopen(%last_good, "lastdb", 0666) die "can't dbmopen lastdb: $!"; $last_good{$name} = time; dbmclose(%last_good) die "can't dbmclose lastdb: $!";

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 { open MAIL, "|mail gera"; print MAIL "bad news: $somename guessed $someguess\n"; close MAIL; return 0; } }

sub init_words { while(defined($filename = glob("*.secret"))) { open(WORDLIST, $filename) die "can't open wordlist: $!"; if(-M WORDLIST >= 7.0) { rename($filename, "$filename.old") die "can't rename $filename to $filename.old: $! "; } while($name = <WORDLIST&gt) { chomp($name); $word = <WORDLIST&gt ; chomp($word); $words{$name} = $word; } close(WORDLIST) die "could not open wordlist: $!"; } }

Листер паролей:

#!/usr/local/bin/perl -w while(defined($filename = glob("*.secret"))) { open(WORDLIST, $filename) die "can't open wordlist: $!"; if(-M WORDLIST >= 7.0) { die "Sorry, the wordlist is older than seven days."; } while($name = <WORDLIST&gt) { chomp($name); $word = <WORDLIST&gt ; chomp($word); write; } close(WORDLIST) die "could not open wordlist: $!"; } format STDOUT = @&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt @&lt&lt&lt&lt&lt&lt&lt&lt @&lt&lt&lt&lt&lt&lt $filename, $name, $word . format STDOUT_TOP = Page @&lt&lt $%

Filename Nname Word ================ ========= ======= .

Программа последнего правильного ввода пароля:

#!/usr/local/bin/perl -w dbmopen(%last_good, "lastdb", 0666) die "can't dbmopen lastdb: $!"; foreach $name (sort keys (%last_good)) { $when = $last_good{$name}; $hours = (time() - $when) / 3600; write; }

format STDOUT = User @&lt&lt&lt&lt&lt&lt&lt&lt&lt&lt: last correct guess was @&lt&lt&lt hour ago. $name, $hours .



Содержание раздела