While 下面的循环在标量 $quit
明显不等于 'j' 后不会停止。
为什么它不会停止?
#!/usr/bin/perl -w
use strict;
my $quit = 'j';
while ($quit eq 'j') {
print "Enter whatever value you want and I bet I still continue.\n";
chomp (my $quit = <STDIN>);
print "quit equals: $quit\n";
}
最佳答案
在循环中,您正在使用 my
关键字创建一个新的 $quit
变量:
chomp (my $quit = <STDIN>);
您实际上想分配给现有变量:
chomp($quit = <STDIN>);
注意 Perl linting 程序,例如 Perl::Critic会提醒您注意这个问题:
Reused variable name in lexical scope: $quit at line 9, column 12. Invent unique variable names. (Severity: 3)
关于perl - 在 perl while 循环中使用 eq 运算符的意外行为。 while循环不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47414952/