我是一個Perl新手,試圖找出爲什麼我的直到條件永遠不會在基本猜數字遊戲中評估爲真。Perl:直到條件從來沒有評估爲真
#!/usr/bin/perl
# Ex 10-1
use warnings;
$num = int(1 + rand 100);
print "\$num is $num\n"; # Debug
print "I'm thinking of a number between 1 and 100. What number is it?\n";
until (chomp($guess = <STDIN>) == $num) {
print "\$guess is $guess\n"; # Debug
if ($guess =~ /\A\s*(quit|exit)?\s*\z/) {
print "Exiting.\n";
exit 0;
} elsif ($guess =~ /\D/) {
print "Invalid input. ";
} else {
($guess < $num) ? (print ("Too low. ")) : (print ("Too high. "));
}
print ("Guess again.\n");
}
print ("Congrats! You guessed the number.\n");
exit 0;
下面是執行的例子:
$ ./ex1
$num is 4
I'm thinking of a number between 1 and 100. What number is it?
5
$guess is 5
Too high. Guess again.
3
$guess is 3
Too low. Guess again.
4
$guess is 4
Too high. Guess again.
比較$猜$ NUM,$猜測等於$ NUM 4時輸入,滿足測試條件。然而,它仍然進入循環,並使其達到最終(默認)打印結果「太高」。
我也試過在chomp($ guess =)周圍加入另一組括號,這沒有幫助。
這是怎麼發生的?
可能的重複[爲什麼這個標量值返回int的1? (chomp)](http://stackoverflow.com/questions/13001954/why-is-this-scalar-value-returning-an-int-of-1-chomp) –