我有以下腳本提示輸入用戶名和密碼並檢查它們是否匹配。但是,即使我只是在提示時輸入輸入,但不輸入用戶名或密碼,它也會打印Success!
。爲什麼?爲什麼我的程序即使在提示處什麼也不輸入的情況下工作?
#!/usr/bin/perl
%data = qw(
javad root
ali 1234
george password
);
print "Please enter your name :\n";
$name = <STDIN>;
chomp($name);
$pass = $data{$name};
#receiving password from user
#compare it with correct password
#print the result
print "What is your password ?\n";
$guess = <STDIN>;
chomp($guess);
$whileTest = "True";
while ($whileTest eq "True") {
if ($pass eq $guess) {
print "Success!\n";
$whileTest = "False";
}
elsif ($pass ne $guess) {
print "Wrong password , Try again :\n";
$guess = <STDIN>;
chomp($guess);
}
}
因爲你實際上並不需要在'%data'中存在'$ name'。因此,'$ data {$ name}'返回[undef](http://perldoc.perl.org/functions/undef.html),這將'eq'一個空密碼。 – Miller
請用戶在您的代碼中正確縮進。我已經清理了它,使其更具可讀性。 – ThisSuitIsBlackNot
'嚴格使用;使用警告;' – squiguy