Hi chacallot
The only thing you're proving is that you havent read the thread am
talking about, or you didnt understand it.
Of course, your "scoping problem" was very interesting (to me),
but why would you have to dive into these arguments ...
In your example, at each iteration the in sub
value is equal to the out sub value +1.
In my example, this is only true after the second iteration.
This is correct like you said.
There is not the slightest chance in C/C++ to
get the desires 'Pseudo-Closure'-behavior of
your former example.
The scoping problem narrows down to an
_initialisation artifact_ of a named
subroutine inside a block.
You can see this clearly if you display
the pointers to the variables which Perl touches:
for (my $i=1;$i<4;$i++) {
my $toto;
sub titi {
my $r0 =\$toto;
$toto++;
print "--> in sub toto=$toto \t($r0)\n";
}
$toto=$i;
my $r1 =\$toto;
print "< before sub toto=$toto \t($r1)\n";
titi;
my $r2 =\$toto;
print ">after sub toto=$toto \t($r2)\n";
}
Prints:
< before sub toto=1 (SCALAR(0x225e98))
--> in sub toto=2 (SCALAR(0x225e98))
after sub toto=2 (SCALAR(0x225e98))
< before sub toto=2 (SCALAR(0x225178)) <--- new iteration
--> in sub toto=3 (SCALAR(0x225e98))
after sub toto=2 (SCALAR(0x225178)) <--- gets destroyed
< before sub toto=3 (SCALAR(0x225178)) <--- new iteration (but same address)
--> in sub toto=4 (SCALAR(0x225e98))
after sub toto=3 (SCALAR(0x225178)) <--- gets destroyed
As you can see, you have closure-like
behavior, the sub alwais hangs on the
initial variable $toto (which gets
reinitialized each iteration) and keeps
that for its lifetime.
Regards
Mirco