下面的循环没有在标量之后停止,$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";
} 发布于 2017-11-21 14:04:04
在循环中,您将使用$quit关键字创建一个新的my变量:
chomp (my $quit = <STDIN>);实际上,您希望为现有变量赋值:
chomp($quit = <STDIN>);注意,Perl linting程序(如Perl::批评家 )会提醒您注意这个问题:
在词法范围内重用变量名:$quit位于第9行第12列。创建唯一的变量名。(严重程度: 3)
发布于 2017-11-21 14:03:36
您可以在循环中重新定义$quit:chomp (my $quit = <STDIN>);删除该行中的my
#!/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 ($quit = <STDIN>);
print "quit equals: $quit\n";
} 发布于 2017-11-21 14:07:07
因为您在while循环中定义了一个新变量$quit。这就是你想要的:
chomp ($quit = <STDIN>);所以,没有我的。
https://stackoverflow.com/questions/47414952
复制相似问题