To ease the understanding of exactly how this might work, assume
that perl 6 is really all continuation passing style under the
surface (parrot is ;-).

        use fatal;
        my $x = do_bar();
        do_foo();

        sub do_bar {
                fail "bah";
        }

The way CPS works is really simple. The principal is that there is
no concept of return. Instead, the call to do_bar, for example, gets
an additional implicit parameter. This parameter is the code ref to
the "rest" of the code, and could look something like this if
deparsed:

        sub ($rv) {
                my $x = $rv;
                do_foo()
        }

So do_bar knows to make a goto to that code ref with it's return
value.

Now, fail gets something similar - it gets the code ref which
applies do_bar's passed in code ref, and it applies it the exception
constructed from "bah".

Fail will also check first to see if the continuation might, at some
level, use fatal. If it does, it takes a dynamically scoped value
instead - the continuation for the last encountered CATCH block.

When it throws the exception, fail will store the continuation it
got (the $x = ... stuff) in the exception object.

CATCH can then choose either to goto it's parent block's
continuation (exit normally), or to goto the continuation stored in
the exception object, which is indirectly the assignment to $x.

Autrijus's example makes this clear (he prefers "resume" over
"continue"):

        use fatal;
        my $x = { 1/0 * 3 CATCH { $!.resume(9) } }; # 27

What's happening is that &infix:</> checks if it's divisor is 0,
and if it is, it will fail. Fail has a continuation to 'multiply by
3 -> exit from block -> assign to $x', and it stores that in the
exception object. Instead of gotoing to that continuation, it will
instead take the last CATCH block that was relevant, in this case
the only one. This one finds the continuation to * 3 etc in the
exception object, and effectively augments the result of
infix:</>(1, 0) to be 9 instead of an undefined value.

I hope this makes things clear.

-- 
 ()  Yuval Kogman <[EMAIL PROTECTED]> 0xEBD27418  perl hacker &
 /\  kung foo master: /me does not drink tibetian laxative tea: neeyah!

Attachment: pgpC58pLTHBic.pgp
Description: PGP signature

Reply via email to