&ipdiscover
- 永远不会调用函数一样,除了当你知道所有的副作用。如有疑问,请做ipdiscover()
。
不要将字符串与==
运算符进行比较:将参数强制转换为数字。如果它看起来不像一个数字,你会得到零。所以$input == "quit"
对于大多数$input
很可能是真的。
但是,if
语句是根据块定义的,而不是以语句形式定义的(如在C中)。因此,你必须做
if ($input eq "quit") {
exit;
}
或者简写:exit if $input eq "quit";
。但你为什么要这么做? exit
终止整个程序。
另一方面,until(($input eq "quit")||($input eq "q"))
是一个正确的终止条件,并且在修复$input
的范围后,将按预期工作。
我想你应该宁可做到以下几点,因为这种处理输入更好的结尾(在Linux例如:一个按Ctrl-d时,Windows;按Ctrl-Z):
use strict; use warnings; # put this at the top of every program!
while(defined(my $answer = prompt("type q or quit to exit: "))) {
last if $answer eq "q"
or $answer eq "quit"
}
sub prompt {
my ($challenge) = @_;
local $| = 1; # set autoflush;
print $challenge;
chomp(my $answer = <STDIN> // return undef);
return $answer;
}
你可以退出循环通过说这是last
迭代。
为什么你在一个条件中使用'=='而另一个在字符串中使用'eq'? – squiguy
它不编译。 “ – Toto
do print”输入I.P地址查找发现名称“。”\ n“; &ipdiscover; 打印“输入q退出并继续任何其他单词”。“\ n”; my $ input =; chomp($ input); ($ input eq“q”)||($ input eq“quit”));如果(($ input eq“q”)) (($ input eq“quit”)||($ input eq“q”)); –
JustCoder