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

gat*_*ina 2 perl

虽然标量$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";

} 
Run Code Online (Sandbox Code Playgroud)

amo*_*mon 6

在循环内部,您将$quit使用my关键字创建一个新变量:

chomp (my $quit = <STDIN>);
Run Code Online (Sandbox Code Playgroud)

您实际上想要分配给现有变量:

chomp($quit = <STDIN>);
Run Code Online (Sandbox Code Playgroud)

请注意,Perl linting程序(如Perl :: Critic)会提醒您注意此问题:

在词法范围内重用变量名:$ quit at第9行,第12列.创建唯一变量名.(严重程度:3)