Hi, Anatoly and I don't know what this bit of code prints:
foo(); foo(); for 1..3 { my $x ::= 3; sub foo { say ++$x }; say ++$x }; Is it 4, 5, 6, 6, 6 or 4, 5, 3, 3, 3? It's almost definitely not 4, 5, 6, 7, 8. I can't rationalize 4, 5, 6, 7, 8 while maintaining the notion that $x is actually lexical. To rationalize the other examples: 4, 5, 6, 6, 6 means that the foo declaration does not capture over an instance of the $x bar, but the actual value in the pad proto itself (the value that will be the default value of newly allocated $x variabless). 4, 5, 3, 3, 3 means that at compile time all variables are instantiated once for BEGIN time captures. Observe: foo(); bar(); for 1..3 { my $x; sub foo { say ++$x } sub bar { say ++$x } say ++$x; } prints 1, 2, 1, 1, 1 because $x is allocated once at compile time and captured into both foo and bar, and then separately allocated once more for each iteration of the loop. If this is indeed the case, then there is a semantics problem: foo(); foo(); for 1..3 { my $x; BEGIN { $x = 3 }; sub foo { say ++$x }; say ++$x }; Must be 4, 5, 1, 1, 1. This is because BEGIN { } and the foo share the same compile time allocated copy of $x, but this is not the copy in the loop. A related issue is: foo(); foo(); for 1..3 { my $x = 10; sub foo { say ++$x }; say ++$x; } Is that 11, 12, 10, 10, 10, or 11, 12, 13, 13, 13, or 1, 2, 10, 10, 10? Lastly, sub foo { my $x; sub { sub { say ++$x } } }; my $bar = foo(); my $gorch = $bar.(); $gorch.(); $gorch.(); my $quxx = $bar.(); $quxx.(); $quxx.(); obviously results in the sequence 0, 1, but does the second call to $bar create a new sequence in $quxx, or is that instance of $x shared between $gorch and $quxx? Intuitively i'd say it is shared, which means that the outer sub declaration implicitly captures $x as well. Can anyone confirm? Obviously my $zot = foo().(); $zot.(); $zot.(); Does create a new sequence. -- Yuval Kogman <[EMAIL PROTECTED]> http://nothingmuch.woobling.org 0xEBD27418
pgpUCL8XSaAwR.pgp
Description: PGP signature