I hate to post something so n00bish, but I'm pretty n00bish when it comes to perl so here it goes:
$var=1;
while(1){
if ($var == 2){
last;
}
$var=2;
}
works the way I intend it to, breaking the infinite loop after the first time through, but:
while(1){
$var=1;
if ($var == 2){
last;
}
$var=2;
}
results in what I can only assume is an infinite loop. My question is what changes scope-wise to the if statement when $var is instantiated outside of the while loop?
update:
Title is supposed to be "spot" not "stop" but apparently I'm dyslexic.
$var=1;
while(1){
if ($var == 2){
last;
}
$var=2;
}
In the above
1) step 1 var =1 then goes to while loop
2) In the while loop in the first check var != 2 so doesnot get into the if loop then sets var = 2
3) In the next attempt var is 2 goes into if loop and the while loop ends
Now lets check the second block of code
while(1){
$var=1;
if ($var == 2){
last;
}
$var=2;
}
1) goes into the while loop then sets var = 1 and checks if var == 2 so doesnot enter into the if loop
2) sets var =2 and then again goes to the while loop begining
3) Now here what is happening is it sets the variable var to 1 again . the condition of var=2 is never met. Hence the infinite loop.
Regarding the scope in the first instance if you print var outside the while loop it would be 1
in the second instance depending on where you declared the variable var like my $var will determine the value.