On Sun Jul 01 14:23:26 2012, sprout wrote: > ...[W]e have (at least) two > variants: > > • Lexical subs close over variables when the name comes into scope > (which could happen multiple times with loops). > • Lexical subs close over variables when referenced or called. > • Lexical subs close over variables when *first* referenced or called. > > For the most part, there is no observable difference here, until it > comes to referential identity and for loops. > > I don’t think we have to worry about referential identity, since > optimisations would make all three variants nearly identical in > that regard. Actually we do. State variables are not persistent across anonymous subroutines. push @subs, sub { state $x } for 1..10; will give you ten subroutines, each with a different $x. I find that to be completely counterintuitive. (It is also completely undocumented.) It means that ‘state $x’ might only create one variable, or might create more than one, depending on what type of sub it is defined in. It also creates yet another set of scoping rules for people to remember. Flip-flops are shared between clones, so why not state variables? The problem it raises for lexical subs is that it is not at all clear when my sub foo { state $x } will create a new $x. If state variables were intended to replace this sort of thing: { my $x; sub fooer { sub { ++$x; foo($x); } } } then they have failed to do so. Is this something we can fix, or will it break too many things? -- Father ChrysostomosThread Next