相关疑难解决方法(0)

对于Loop和Lexically Scoped变量

版本#1

use warnings;
use strict;

my $count = 4;

for $count (1..8) {
    print "Count = $count\n";
    last if ($count == 6);
}

if (not defined($count)) {
    print "Count not defined\n";
}
else {
    print "Count = $count\n";
}
Run Code Online (Sandbox Code Playgroud)

这打印:

1
2
3
4
5
6
4
Run Code Online (Sandbox Code Playgroud)

为什么?因为for循环$count在其块内创建了自己的词法范围版本.

版本#2

use warnings;
use strict;

my $count;
for $count (1..8) {
    print "Count = $count\n";
    last if ($count == 6);
}

if (not defined($count)) {
    print "Count not …
Run Code Online (Sandbox Code Playgroud)

perl

4
推荐指数
2
解决办法
2887
查看次数

标签 统计

perl ×1