Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

354 lines (322 sloc) 9.566 kb
id: lambda:«->»
token: ->
syn: -> SIGNATURE { STATEMENTS }
name: lambda
desc: introduces a (possibly empty) signature to a block
ex: for @list -> $a { say $a; }
my &function := -> { say 42; };
id: lambda:«<->»
token: <->
syn: <-> SIGNATURE { STATEMENTS }
name: lambda rw
desc: introduces a (possibly empty) signature to a block, applying the
'is rw' trait on all arguments
ex: for @list <-> $a { $a++ }
id: block
token: { }
syn: { <statemts> }
name: block
# XXX what kind of scope?
desc: groups statements and introduces a new scope
ex: for @list -> $a { say $a }
id: regex_block
token: { }
syn: regex { <regex> }
name: regex block
desc: delimits a regex, rule or token
ex: regex word { <alpha>+ }
seealso: regex_declarator:regex
seealso: regex_declarator:token
seealso: regex_declarator:rule
id: label
token: :
syn: IDENTIFIER:
name: label
desc: assigns a name to a block or statement
ex: INNER:
for @list {
if m/something/ {
last INNER;
}
}
# XXX is this the right place for this?
id: eat_terminator
token: ;
syn: STATEMENT;
name: statement terminator
desc: terminates a statement. Optional for the last statement in a
block or file.
ex: say $a;
say $b;
id: statement_control:use
token: use
syn: use MODULE EXPRESSION;
name: use
desc: Load a module, class, pragma or language
ex: use Test;
ex: {
use v5;
# perl 5 code here
}
# Perl 6 code here
id: statement_control:no
token: no
syn: no MODULE EXPRESSION;
name: no
desc: Unloads a module or class, or disables a pragma
ex: no Test;
seealso: statement_control:use
id: statement_control:if
token: if elsif else
syn: if EXPRESSION BLOCK
syn: if EXPRESSION BLOCK else BLOCK
syn: if EXPRESSION elsif EXPRESSION BLOCK
name: if
desc: executes a code block only if an yields True.
There can be an arbitrary number of elsif blocks, and one or no
else block
ex: if $a < $b {
say "b is larger than a";
}
ex: if $a < $b {
say "b is smaller than a";
} elsif $a == $b {
say "b is as large as a";
} else {
say "b is larger than a"
}
id: statement_control:unless
token: unless
syn: unless EXPRESSION BLOCK
name: unless
desc: executes a code block only if an expression yields False.
Unlike the if-statement no else-block is allowed
ex: unless $allowed {
die "Insufficent permissions, aborting"
}
seealso: statement_control:if
id: statement_control:while
token: while
syn: while EXPRESSION BLOCK
name: while
desc: executes a code block as long as a controlling expression yields True
ex: while $a < $b {
$a *= 2;
}
say $a;
id: statement_control:until
token: until
syn: until EXPRESSION BLOCK
name: until
desc: executes a code block as long as a controlling expression yields False
ex: until $a > $b {
$a *= 2;
}
say $a;
id: statement_control:repeat
token: repeat
syn: repeat BLOCK while EXPR
syn: repeat BLOCK until EXPR
syn: repeat while EXPR BLOCK
syn: repeat until EXPR BLOCK
name: repeat
desc: Repeatedly executes a block controlled by a condition
ex: my $in;
repeat {
say "greet me"
$in = =$*IN;
} until $in ~~ m/hi|hello|cheers/
id: statement_control:loop
token: loop
syn: loop(EXPR1; EXPR2; EXPR3) BLOCK
name: loop
desc: C-Style for-Loop.
It is roughly equivalent to EXPR1; while EXPR2 { BLOCK; EXPR3 }
ex: loop(my $i = 1; $i < $limit; $i *= 2){
say $i;
}
id: statement_control:for
token: for
syn: for LIST PBLOCK
name: for
desc: Iterate over LIST, and execute the block for each item
ex: for <a b c d e> -> $a {
say $a;
}
ex: for @list Z 0 .. * -> $item, $index {
say "The item No. $index is '$item'";
}
ex: for @list {
.say # use $_ as implicit topic
}
id: statement_control:given
token: given
syn: given EXPR BLOCK
name: given
desc: Sets the topic ($_) in BLOCK to EXPR. Sets item context to EXPR.
ex: given @list {
.sort.join('|').say
}
seealso: statement_control:when
seealso: statement_control:default
id: statement_control:when
token: when
syn: when EXPRESSION BLOCK
name: when
desc: does a smartmatch of the current topic ($_) against EXPRESSION
and executes BLOCK if the match returned True.
ex: given $greeting {
when rx:i{dear} { say "friendly" }
when rx:i{hi} { say "informal" }
when rx:i{hello} { say "neutral" }
default { say "unclassified greeting" }
}
seealso: statement_control:given
seealso: statement_control:default
id: statement_control:default
token: default
syn: default BLOCK
name: default
desc: executes BLOCK if no 'when'-block mached in the current scope
seealso: statement_control:when
seealso: statement_control:given
# TODO: BEGIN, CHECK, INIT, ...
id: version
token: v .
name: version
desc: string
ex: v1.2
ex: v3.4+
ex: v0.*.3
id: fatarrow
token: =>
syn: KEY => VALUE
name: pair
token: =>
desc: constructs a pair, usually building a hash or named arguments
ex: my %continents = (
England => 'Europe',
Brazil => 'South America',
India => 'Asia'
);
ex: say @list.grep(matcher => &my_function);
id: scope_declarator:my
token: my
syn: my VARIABLE
name: my
desc: declares a lexically scoped variable
ex: my $var = 3;
ex: {
my $x;
# 'my' variable $x is visible in this block
}
# no $x here.
ex: my %hash;
id: scope_declarator:our
token: our
syn: our VARIABLE
name: our
desc: declares a package scoped variable
ex: our @foo;
id: scope_declarator:state
token: state
syn: state VARIABLE
name: state
desc: declares a lexically scoped variable whos scope is presevered across
multiple executions of the block
ex: sub iterator {
state $c = 0; # assignment only executed at first call
return ++$c;
}
say iterator(); # prints 1
say iterator(); # prints 2
say iterator(); # prints 3
id: scope_declarator:constant
token: constant
syn: constant VARIABLE = VALUE;
name: constant
desc: declares a lexically scoped constant. Assignment happens at compile
time.
ex: constant $pi = 3.14159;
id: scope_declarator:has
token: has
syn: has VARIABLE;
syn: has VARIABLE = VALUE;
name: has
desc: declares an object attribute, i.e. a variable that is stored
in every object of a class.
Only makes sense in classes, roles and grammars.
ex: class Perlhacker {
has $!brain;
has @.modules_on_cpan;
}
seealso: package_declarator:class
id: package_declarator:class
token: class
syn: class CLASSNAME TRAITS; CLASSDEF
syn: class CLASSNAME TRAITS { CLASSDEF }
name: class
desc: declares a class. The TRAITS are optional. If the class declaration
ends with a semicolon ';' it expands over the rest of the file.
If it is followed by a curly brace, everything up to the closing brace
is considered to be the class definition.
A class comes with its own namespaces (with the same name as the class)
ex: class Array is also {
method length {
die "'Length' is a forbidden word in Perl 6";
}
}
ex: class Dog {
has $.name;
has @.legs;
method bark {
say "bark";
}
}
seealso: scope_declarator:has
seealso: routine_declarator:method
id: package_declarator:grammar
token: grammar
syn: grammar CLASSNAME TRAITS; CLASSDEFF
syn: grammar CLASSNAME TRAITS { CLASSDEF }
name: grammar
desc: declares a grammar, i.e a class that is designed to hold regexes,
rules and tokens.
ex: grammar URL {
regex TOP {
<schema>
<host>?
<path>
}
token schema {
\w+ ':'
}
token host { ... }
...
}
seealso: regex_declarator:token
seealso: regex_declarator:rule
seealso: regex_declarator:regex
seealso: package_declarator:class
id: package_declarator:module
token: module
syn: module MODULENAME MODULEDEF
syn: module MODULENAME TRAITS MODULEDEF
name: module
desc: declares a module, which is a package with a bit of extra semantics
TODO: find out what the difference between a package and a module
is, and explain it
# TODO: explain 'package'
id: package_declarator:role
token: role
syn: role ROLENAME ROLEDEF
name: role
desc: declares a role, which is a collection of methods and attributes,
or a class of which no objects can be instantiated directly. Some
other languages call these things "mixins". Roles are intended to be
the primary means of object-oriented code reuse in Perl 6.
ex: role Pet {
has $.owner;
method feed($food) { ... }
}
# TODO: more examples for roles
Jump to Line
Something went wrong with that request. Please try again.