Added: Dec 1, 2008

From: h4ck3rm1k3

Duration: 5:44

http://www.perlfoundation.org/perl6 - - allowed to be a junction of types: sub foo returns Int|Str {...} The "will" syntax specifically introduces a closure trait without requiring the extra parens that "is" would. Saying: will flapdoodle { flap() and doodle() } is exactly equivalent to: is flapdoodle({ flap() and doodle() }) but reads a little better. More typically you'll see traits like: will first { setup() } will last { teardown() } The final block of a subroutine declaration is the "do" trait. Saying: sub foo { ... } is like saying: sub foo will do { ... } Note however that the closure eventually stored under the "do" trait may in fact be modified in various ways to reflect argument processing, exception handling, and such. We'll discuss the "of" and "returns" traits later when we discuss types. Back to syntax. The "sub" form A subroutine can be declared as lexically scoped, package scoped, or unscoped: rule lexicalsub :w { [lexscope] [type]? [subintro] [subname] [psignature]? [trait]* [block] } rule packagesub :w { [subintro] [subname] [psignature]? [trait]* [block] } rule anonsub :w { [subintro] [psignature]? [trait]* [block] } The non-lexically scoped declaration cannot specify a return type in front. The return type can only be specified as a trait in that case. [Update: These days the return type may be specified as part of the signature after a "--]". It's also possible to use a declarator in front of a declaration that introduces no name. And again, the ":w" is no longer used in rules.] As in Perl 5, the difference between a package sub and an anonymous sub depends on whether you specify the "[subname]". If omitted, the declaration (which is not really a declaration in that case) generates and returns a closure. (Which may not *really* be a closure if it doesn't access any external lexicals, but we call them all closures anyway just in case...) A lexical subroutine is declared using either "my" or "our": rule lexscope { my | our } This list doesn't include "temp" or "let" because those are not declarators of lexical scope but rather operators that initiate dynamic scoping. See the section below on Lvalue subroutines for more about "temp" and "let". In both lexical and package declarations, the name of the subroutine is introduced by the keyword "sub", or one of its variants: rule subintro { sub | method | submethod | multi | rule | macro } A "method" participates in inheritance and always has an invocant (object or class). A "submethod" has an invocant but does not participate in inheritance. It's a sub pretending to be a method for the current class only. A "multi" is a multimethod, that is, a method that is called like a subroutine or operator, but is dispatched based on the types of one or more of its arguments. [Update: These days "multi" is just a modifier on "sub" or "method", but if you say "multi" the "sub" may be omitted since it's the default.] Another variant is the regex "rule", which is really a special kind of method; but in actuality rules probably get their own set of parse rules, since the body of a rule is a regex. I just put "rule" into [subintro] as a placeholder of sorts, because I'm lazy. [Update: Rules are now split up into "regex", "token", and "rule" declarations. These differ in backtracking policy and whitespace matching. See S05.] A "macro" is a subroutine that is called immediately upon completion of parsing. It has a default means of parsing arguments, or it may be bound to an alternate grammar rule to parse its arguments however you like. These syntactic forms correspond the various "Routine" types in the "Code" type hierarchy: Code ____________|_ _______________ | | Routine Block __________ ______|______________ _ __|___ | | | | | | | | Sub Method Submethod Multi Rule Macro Bare Parametric The "Routine"/"Block" distinction is fairly important, since you always "return" out of the current "Routine", that is, the current "Sub", "Method", "Submethod", "Multi", "Rule", or "Macro". Also, the &_ variable refers to your current "Routine". A "Block", whether "Bare" or "Parametric", is invisible to both of those notions. (It's not yet clear

Channel: Education

Tags: apocalypse  larry  perl  perl6  wall 


Rating: ( ratings)    Views: 7    Comments: 0