Author: lwall Date: 2009-10-09 01:51:23 +0200 (Fri, 09 Oct 2009) New Revision: 28668
Modified: docs/Perl6/Spec/S06-routines.pod docs/Perl6/Spec/S12-objects.pod Log: [S06,S12] clarify when yada is taken to be a stub [S06] clarify semantics of package subs [S06] document that subs default to 'my' scope now Modified: docs/Perl6/Spec/S06-routines.pod =================================================================== --- docs/Perl6/Spec/S06-routines.pod 2009-10-08 22:55:49 UTC (rev 28667) +++ docs/Perl6/Spec/S06-routines.pod 2009-10-08 23:51:23 UTC (rev 28668) @@ -16,8 +16,8 @@ Created: 21 Mar 2003 - Last Modified: 3 Oct 2009 - Version: 117 + Last Modified: 8 Oct 2009 + Version: 118 This document summarizes Apocalypse 6, which covers subroutines and the new type system. @@ -100,6 +100,14 @@ my @subs = (sub foo { ... }, sub bar { ... }); +Another difference is that subroutines default to C<my> scope rather +than C<our> scope. However, subroutine dispatch searches lexical +scopes outward, and subroutines are also allowed to be I<postdeclared> +after their use, so you won't notice this much. A subroutine that is +not declared yet may be called using parentheses around the arguments, +in the absence of parentheses, the subroutine call is assumed to take +multiple arguments in the form of a list operator. + =head2 Anonymous subroutines The general syntax for anonymous subroutines is: @@ -248,10 +256,14 @@ you must explicitly use the "C<supersede>" declarator. (The compiler may refuse to do this if it has already committed to the previous definition.) -The C<...> is the "yadayadayada" operator, which is executable but returns -a failure. You can also use C<???> to produce a warning, or C<!!!> to -always die. These also officially define stub blocks if used as the -only expression in the block. +The C<...> is the "yadayadayada" operator, which is executable but +returns a failure. You can also use C<???> to produce a warning, +or C<!!!> to always die. These also officially define stub blocks. +Any of these yada operators will be taken as a stub if used as the main +operator of the first statement in the block. (Statement modifiers +are allowed on that statement.) The yada operators differ from their +respective named functions in that they all default to a message +such as: "Unimplemented stub of sub foo was executed". It has been argued that C<...> as literal syntax is confusing when you might also want to use it for metasyntax within a document. @@ -265,15 +277,18 @@ =head2 Globally scoped subroutines -Subroutines and variables can be declared in the global namespace, and are -thereafter visible everywhere in a program via the GLOBAL package. They -may be made directly visible by importation. +Subroutines and variables can be declared in the global namespace +(or any package in the global namespace), and are thereafter visible +everywhere in the program via the GLOBAL package (or one of its +subpackages). They may be made directly visible by importation, +but may not otherwise be called with a bare identifier, since subroutine +dispatch only looks in lexical scopes. Global subroutines and variables are normally referred to by prefixing their identifiers with the C<*> twigil, to allow contextual overrides. GLOBAL::<$next_id> = 0; - sub GLOBAL::saith($text) { print "Yea verily, $text" } + sub GLOBAL::saith($text) { say "Yea verily, $text" } module A { my $next_id = 2; # hides any global or package $next_id @@ -293,11 +308,21 @@ saith($next_id); # Unambiguously the global definitions } +Despite the fact that subroutine dispatch only looks in lexical scopes, you +can always call a package subroutine directly if there's a lexical alias +to it, as the C<our> declarator does: + + module C; + our sub saith($text) { say "Yea verily, $text" } + saith("I do!") # okay + C::saith("I do!") # also okay + =head2 Dynamically scoped subroutines Similarly, you may define contextual subroutines: my sub myfunc ($x) is context { ... } + my sub &*myfunc ($x) { ... } # same thing This may then be invoked via the syntax for contextual variables: Modified: docs/Perl6/Spec/S12-objects.pod =================================================================== --- docs/Perl6/Spec/S12-objects.pod 2009-10-08 22:55:49 UTC (rev 28667) +++ docs/Perl6/Spec/S12-objects.pod 2009-10-08 23:51:23 UTC (rev 28668) @@ -49,10 +49,11 @@ The first form is allowed only as the first declaration in a compilation unit (that is, file or eval string). -If the class body consists of a single statement consisting of a +If the class body begins with a statement whose main operator is a single C<< prefix:<...> >> (yada) listop, the class name is introduced without a definition, and a second declaration of that class in the -same scope does not complain about redefinition. Thus you may +same scope does not complain about redefinition. (Statement modifiers +are allowed on such a C<...> operator.) Thus you may forward-declare your classes: class A {...} # introduce A as a class name without definition