【发布时间】:2014-02-24 14:25:58
【问题描述】:
我有一个循环,通过检查文件数是否恒定来检查目录中的所有文件是否都上传到目录。
开始,它知道目录中有 $before 文件(“之前”它再次查找)。
use strict;
use warnings;
my $after = 0;
my $before ="10";
until($before == $after) {
#check again, which changes the value of $after.
#Now....
$after = "20";
if ( $after == $before ) {
print "After is before. Moving out of until because all files are there!\n";
}
else {
print "After isn't before.\n";
my $before = $after;#set $before to be the new value after the update
my $after = 0; #this will be updated in the next update
sleep 1;
}
}
当我运行它时,它声称它在 else{} 中将 $before 设置为 $after,但实际上,$before 仍然是 10,就像它之前设置的那样,并且程序无限循环。
当我从else{} 内部删除mys 时,脚本运行正常:
use strict;
use warnings;
my $after = 0;
my $before ="10";
until( $before == $after ) {
#check again, which changes the value of $after.
#Now....
$after = "20";
if($after == $before) {
print "After is before. Moving out of until because all files are there!\n";
}
else {
print "After isn't before.\n";
$before = $after;#set $before to be the new value after the update
$after = 0; #this will be updated in the next update
sleep 1;
}
}
这是否意味着在 else 中定义为“my $before”的$before 与上面定义的“$before”不是同一个变量?
【问题讨论】:
-
使用
$a和`$b 作为变量名是个糟糕的主意 -
use strict和use warnings -
代码中的
&update_a是什么? -
你显示的代码没有明显的问题,所以我猜问题出在你没有显示的代码上。如果您无法想出一个展示问题的可运行示例,我们可能无法提供帮助。
标签: perl variables loops conditional