다음 스크립트는 사용자 이름과 암호를 묻는 메시지를 표시하고 일치하는지 확인합니다. 그러나, 방금 을 누르더라도 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);
}
}
실제로'$ name'이'% data'에 존재하지 않기 때문에. 그러므로'$ data {$ name}'은 [undef] (http://perldoc.perl.org/functions/undef.html)를 반환하며, 빈 암호를'eq'합니다. – Miller
코드에 적절한 들여 쓰기를하십시오. 좀 더 읽기 쉽도록 정리했습니다. – ThisSuitIsBlackNot
'use strict; ' – squiguy