Jonathan Scott Duff wrote:

On Thu, Oct 02, 2003 at 11:39:20AM +0100, Dave Mitchell wrote:

On Thu, Oct 02, 2003 at 04:15:06AM -0600, Luke Palmer wrote:

So the question is: What happens when indexof isn't on the call chain,
but that inner closure is?

But how can the inner closure be called if not via indexof?


I believe that's exactly what Luke's original example was
illustrating.

On Thu, Oct 02, 2003 at 01:59:26AM -0600, Luke Palmer wrote:

So, I must ask, what does this do:

   sub foo() {
       return my $self = {
           print "Block";
           return $self;
       }
   }


foo() returns a closure that contains code that returns from the foo()
subroutine (the line that says "return $self")  When that closure is
then called ...


   my $block = foo;
   print "Main";
   $block();


... foo() is no longer executing.


That is, the block returns from a function that's not currently
executing.

Will the output be: a)

Can't 'return' from closure Block

b)

   Main
   Block
   Can't 'return' from closure Block

(just like (a) but runtime)
c)

   Main
   Block

(the block's return returns from the main program, or whatever function
is currently executing)

d)

   Main
   Block
   Main
   Block
   Main
   Block
   ...

(the block closes over the function's return continuation)


I would expect (a) to happen, but (d) has some interesting
possibilities.  And wouldn't (d) be:

        Main
        Block
        Block
        Block
        ...

?

Actually, if your last parenthetical were true, it would be

        Main
        Block
        End

because though foo()'s return continuation is closed over, it only
gets executed once and then returned.  I.e., to get "Block" again,
you'd need to execute the return value of $block.

        my $block = foo;
        print "Main";
        $b2 = $block();
        $b3 = $b2();
        $b4 = $b3();                    # etc.
        print "End";

or for the infinite version:

        my $block = foo;
        print "Main";
        $block = $block() while 1;
        print "End";                  # we never get here

Or am I missing something?

Possibly, I brought a similar example to Larry a while ago and he said that creating a closuer and assigning to a variable (or even returning it), may need to be an exception to the "sub" required for "return" rule, exactly because of this dangling context problem. So in the above example, the return is from the local closure not the from foo().

--
[EMAIL PROTECTED]
[EMAIL PROTECTED]



Reply via email to