On Fri, Aug 03, 2007 at 03:37:39PM -0700, Bob Rogers wrote: > A naive PIR implementation of this loop is included as the second > attachment. It fails miserably, because PIR can't distinguish between > the different scopes for "$i" and "$n". > > sub make_closures_loop { > # Return n closures, each with lexical references to $i and $n. > my $n = shift; > > my @result; > for (1..$n) { > my $i = $_; > push(@result, sub { print "Called sub $i out of $n.\n"; }); > } > return @result; > } > > [...] > > ## Return n closures, each with lexical references to "I" and "N'. > .sub make_closures_loop > .param pmc n > .lex "$n", n > .lex "$i", $P42 > [...] > > .sub internal_make_closures_loop_0 :outer('make_closures_loop') > find_lex $P42, "$i" > find_lex $P43, "$n" > [...]
This seems wrong to me -- shouldn't $i be scoped to the internal loop instead of the outer one? In other words, I think the PIR code should read: .sub make_closures_loop .param pmc n .lex "$n", n ... and then .sub internal_make_closures_loop_0 :outer('make_closures_loop') .lex "$i", $P42 find_lex $P43, "$n" ... Otherwise, the reason that PIR isn't able to distinguish the scopes is because in PIR you're actually defining them to be in the same scope. However, I must confess that I still haven't grokked all of the ramifications of lexicals and closures in PIR yet -- and I may even be interpreting the p5 translation incorrectly. It just looks to me as though the "naive translation" isn't being faithful to the original p5 source, and so perhaps that's causing the problem you're seeing. Hope this helps, Pm