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
在其块内创建了自己的词法范围版本.
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 ×1