Yep, this is the issue that I wrote about a couple of days ago. If you take a look at the code though, there is a point where the block for bar is being made into a closure. Take a look at sub _block14:

.sub "_block14"  :outer("_block12")
  get_global $P20, "_block15"
  newclosure $P20, $P20
  $P19 = $P20()
  .return ($P19)
.end

The _block14 symbol is what is being stored, but _block15 is the actual body of the "bar" function. So you can *almost* solve your problem by calling _block14 in _block12 and saving the result as the global "bar". I say almost because you won't be able to call _block14 either :) Before you can call _block14 you need to bind it to an environment using newclosure (notice that that is what it is doing for _block15).

Andrew Parker

On Feb 6, 2008, at 4:19 PM, Patrick R. Michaud wrote:

On Wed, Feb 06, 2008 at 03:33:14PM +0100, Klaas-Jan Stol wrote:
function foo()

  local a = 2

  function bar()
      print(a)
  end
end

foo()
bar()

What happens here is, a function foo is defined, in which a local var. "a" is initialized to the value "2". Another function is defined, called "bar", which prints the value of variable a. As "bar" is lexically nested within function foo, "bar" has access to "a". Note that "bar" is only defined, it's not run when "foo" is active. (function foo() ... can be rewritten as "foo =
function()..." )

Now, after defining "foo", foo is invoked: foo(). This will actually create the lexical "a", and define "bar". After running foo, there should be a
function "bar".
So now we can invoke "bar": bar(). When running bar, the value of the
lexical "a" should be printed.

This doesn't work. It seems to me that the generated PIR is correct. I'm not
clear why this doesn't work.
Anybody who sees an obvious error I made?

It has to do with Parrot's handling of closures.  The problem is
that 'bar' isn't being stored as a Closure PMC.  Somewhere in the
generated PIR we need to have a 'newclosure' operation on the
code representing the "bar" function to create a Closure PMC and
store that in 'bar'.

This is likely related to the earlier message in the thread about
overall handling of closures in PCT, for which I'll get a more detailed
response out shortly.

Pm


Reply via email to