On Fri, Jun 17, 2005 at 10:13:54PM +0200, Marcus Holland-Moritz wrote: > On 2005-06-15, at 14:43:11 +0200, Rafael Garcia-Suarez wrote: > > > Jeff 'japhy' Pinyan wrote: > > > On Jun 15, Paul Marquess said: > > > > > > > Consider this code > > > > > > > > for (1 .. 3) { > > > > my $outer = 0 ; > > > > > > > > sub mkClosure { sub { $outer = 1234 } } > > > > > > > > &{ mkClosure() }(); > > > > > > > > print "outer $outer\n" ; > > > > } > > > > > > I'd say the proper behavior would be to print 1234, 0, and 0. This is > > > because the mkClosure function, while defined physically inside the > > > block, > > > is defined once only, and contains a closure to the lexical $outer. > > > Because of that, the memory for $outer the first time through the loop > > > cannot be freed, so the 'my $outer' will use a different memory space for > > > it the second and third times through. > > > > Your analysis sounds right, but I'd like to know which change changed > > this. > > Change 19637 by [EMAIL PROTECTED] on 2003/05/29 18:47:40 > > Subject: [PATCH] jumbo closure fix > From: Dave Mitchell <[EMAIL PROTECTED]>
Yes, one of the changes this patch makes is that named subs now close on behalf of inner anon subs; ie in my $x; sub foo { sub { $x } } at compile-time, foo() captures the first instance of $x; when foo() is later called, the new inner anon closure is provided with the instance of $x saved for it by foo(). In this respect, sub foo { ... } now more closely mimicks BEGIN { *foo = sub { ... } } when it comes to closures -- You're only as old as you look