perl - 在 perl while 循环中使用 eq 运算符的意外行为。 while循环不会停止

标签 perl

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/

相关文章:

regex - 可选的正则表达式匹配?

mysql - 最新行在选择中不可用...以进行更新

perl - awk:打印一组行中的三行

linux - 如何在 perl 中更改目录及其文件的 mtime?

perl - 读取两个 YAML 文件并比较两者中出现的模块

perl - 如何在html树中向上移动节点并提取链接?

windows - PerlIO::encoding 的意外行为

perl - 如何延迟加载 Perl 变量?

php - 客户端/服务器通信的最佳协议(protocol),从 PHP/Perl 到 C++/Qt4

perl - 手动安装 Perl 模块