0
我已經編寫了一個代碼,該代碼嘗試使用perl腳本存儲用戶的上次登錄時間,並將其存儲在使用dbmopen的散列中,但它不會將我更新登錄時間: - 我的代碼去如下: -嘗試使用perl腳本將用戶的上次登錄時間存儲在數據庫中
#!/usr/bin/perl -w
use DB_File;
use Time::localtime;
%last_good =();
%words =() ;
init_words();
print " What is your name ? \n";
$name=<STDIN>;
chomp($name);
print "Hi $name , please Enter your secret password: \n" ;
$passwd = <STDIN>;
chomp($passwd);
$i=1;
while(! good_password($name,$passwd))
{
print " Wrong Password , Please Try Again : \n";
$passwd=<STDIN>;
chomp($passwd);
}
print "assigning the last login time \n ";
dbmopen (%last_good,"lastdb",0777) || die "can't dbmopen lastdb: $!";
$last_good{$name} = localtime; // update last login time if password correct
dbmclose (%last_good) || die "can't dbmclose lastdb: $!";
print "the last login time for : $name is $last_good{$name} ";
sub init_words
{
open(WORDLIST,"password.txt") || die " Can not Read the password.txt : $! \n";
while(defined ($name = <WORDLIST>))
{
chomp($name);
$name =~ s/\W.*// ;
$name =~ tr /A-Z/a-z/ ;
$password=<WORDLIST>;
chomp($password);
$words{$name}=$password;
if(! defined $last_good{$name})
{
dbmopen (%last_good,"lastdb",0777) || die "can't dbmopen lastdb: $!";
$last_good{$name} = localtime; // update login time for 1st login
dbmclose (%last_good) || die "can't dbmclose lastdb: $!";
}
write;
}
close(WORDLIST) || die " Could not close the file : $! \n ";
}
什麼,我要做的是更新或將在數據庫中的第一次登錄時間如果不是他們 使用if(! defined $last_good{$name})
,然後在情況下,它已經定義當我第二次運行prog時,我希望它從數據庫中讀取,而不是嘗試再次創建它。但是,當我第一次分配後嘗試打印$last_good{$name} = localtime;
時,它不打印登錄localtime
,但它表示它未定義。
'good_password'是否也使'$ name'小寫?另外,我不明白你用'$ name =〜s/\ W。* //'完成了什麼。嘗試使用'Data :: Dumper'類轉儲'%last_good'來驗證它看起來像你期望的。 – ElPaco