Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[svn:perl6-synopsis] r13583 - doc/trunk/design/syn

3 views
Skip to first unread message

la...@cvs.perl.org

unread,
Feb 12, 2007, 1:01:19 PM2/12/07
to perl6-l...@perl.org
Author: larry
Date: Mon Feb 12 10:01:18 2007
New Revision: 13583

Modified:
doc/trunk/design/syn/S04.pod
doc/trunk/design/syn/S06.pod

Log:
Decided "leave" wasn't so bad after all, given the new syntax refactoring.
And "give" can easily be confused with "given". Please don't sue for whiplash.


Modified: doc/trunk/design/syn/S04.pod
==============================================================================
--- doc/trunk/design/syn/S04.pod (original)
+++ doc/trunk/design/syn/S04.pod Mon Feb 12 10:01:18 2007
@@ -653,33 +653,33 @@
and bare closures are transparent to C<return>. If you pass a closure
object outside of its official "sub" scope, it is illegal to
return from it. You may only leave the displaced closure block itself
-by falling off the end of it or by explicitly calling C<give>.
+by falling off the end of it or by explicitly calling C<leave>.

To return a value from any pointy block or bare closure, you either
just let the block return the value of its final expression, or you
-can use C<give>, which comes in both function and method forms.
+can use C<leave>, which comes in both function and method forms.
The function (or listop) form always exits from the innermost block,
returning its arguments as the final value of the block exactly as
return does. The method form will leave any block in the dynamic
-scope that can be named as an object and that responds to the C<.give>
+scope that can be named as an object and that responds to the C<.leave>
method.

-Hence, the C<give> function:
+Hence, the C<leave> function:

- give(1,2,3)
+ leave(1,2,3)

is really just short for:

- $?BLOCK.give(1,2,3)
+ $?BLOCK.leave(1,2,3)

To return from your immediate caller, you can say:

- caller.give(1,2,3)
+ caller.leave(1,2,3)

Further contexts up the caller stack may be located by the selector
that is built into the C<caller> function itself:

- caller({ .label eq 'LINE' }).give(1,2,3);
+ caller({ .label eq 'LINE' }).leave(1,2,3);

By default the innermost dynamic scope matching the selection criteria
will be exited. This can be a bit cumbersome, so in the particular
@@ -687,7 +687,7 @@
scope is considered a kind of pseudo object specifying a potential
dynamic context. If instead of the above you say:

- LINE.give(1,2,3)
+ LINE.leave(1,2,3)

it was always exit from your lexically scoped C<LINE> loop, even
if some inner dynamic scope you can't see happens to also have that
@@ -707,7 +707,7 @@
then that outer loop is the one that must be controlled. In other words,
it first tries this form:

- LINE.give(1,2,3)
+ LINE.leave(1,2,3)

If there is no such lexically scoped outer loop in the current subroutine,
then a fallback search is made outward through the dynamic scopes in

Modified: doc/trunk/design/syn/S06.pod
==============================================================================
--- doc/trunk/design/syn/S06.pod (original)
+++ doc/trunk/design/syn/S06.pod Mon Feb 12 10:01:18 2007
@@ -1713,7 +1713,7 @@
# want.arity === 0
# want.count === 2

-=head2 The C<give> function
+=head2 The C<leave> function

As mentioned above, a C<return> call causes the innermost surrounding
subroutine, method, rule, token, regex (as a keyword) or macro to
@@ -1721,9 +1721,10 @@
may be returned from. You may not use C<return> to return from loops,
bare blocks, pointy blocks, or quotelike operators such as C<rx//>.

-To return from these types of code structures, the C<give> method
-is used instead. (It can be taken to mean either "give up" or
-"bequeath" as appropriate.) The object specifies the scope to exit,
+To return from these types of code structures, the C<leave> method
+is used instead. (It can be taken to mean either "go away from" or
+"bequeath to your successor" as appropriate.)
+The object specifies the scope to exit,
and the method's arguments specify the return value. If the object
is omitted (by use of the function or listop forms), the innermost
block is exited. Otherwise you must use something like C<caller>
@@ -1736,25 +1737,37 @@
As with C<return>, the arguments are taken to be a C<Capture> holding the
return values.

- give; # return from innermost block of any kind
- caller(Method).give; # return from innermost calling method
- &?ROUTINE.give(1,2,3); # Return from current sub. Same as: return 1,2,3
- &?ROUTINE.give <== 1,2,3; # same thing, force return as feed
- OUTER.give; # Return from OUTER label in lexical scope
- &foo.give: 1,2,3; # Return from innermost surrounding call to &foo
+ leave; # return from innermost block of any kind
+ caller(Method).leave; # return from innermost calling method
+ &?ROUTINE.leave(1,2,3); # Return from current sub. Same as: return 1,2,3
+ &?ROUTINE.leave <== 1,2,3; # same thing, force return as feed
+ OUTER.leave; # Return from OUTER label in lexical scope
+ &foo.leave: 1,2,3; # Return from innermost surrounding call to &foo

Note that these are equivalent in terms of control flow:

- COUNT.give;
+ COUNT.leave;
last COUNT;

However, the first form explicitly gives the return value for the
entire loop, while the second implicitly returns all the previous
-successful loop iteration values as a list comprehension. A C<give>
+successful loop iteration values as a list comprehension. (It may,
+in fact, be too late to give a return value for the loop if it is
+being evaluated lazily!) A C<leave>
from the inner loop block, however, merely specifies the return value for
that iteration:

- for 1..10 { give $_ * 2 } # 2..20
+ for 1..10 { leave $_ * 2 } # 2..20
+
+Note that this:
+
+ leave COUNT;
+
+will always be taken as the function, not the method, so it returns
+the C<COUNT> object from the innermost block. The indirect object form
+of the method always requires a colon:
+
+ leave COUNT: ;

=head2 Temporization

0 new messages