@@ -20,6 +20,7 @@ Note that it is still allowed to have a name
20
20
# anonymous, but knows its own name
21
21
my $s = anon sub triple($x) { 3 * $x }
22
22
say $s.name; # triple
23
+ say triple(42); # Undeclared routine: triple
23
24
24
25
= head1 Adverb
25
26
@@ -38,7 +39,10 @@ reason colon pair notation is also known as the adverbial pair form:
38
39
:a(4) # Same as "a" => 4
39
40
40
41
Some other forms that use a colon in ways that have adverb-like semantics
41
- are called adverbial forms.
42
+ are called adverbial forms. One special form starts with an integer value,
43
+ followed by a name (for the key):
44
+
45
+ :20seconds # same as seconds => 20
42
46
43
47
= head1 Autothreading
44
48
@@ -49,7 +53,7 @@ these calls is assembled in a junction of the same type as the original
49
53
junction.
50
54
51
55
sub f($x) { 2 * $x };
52
- if f(1|2|3) == 4 {
56
+ if f(1|2|3) == 4 { # same as f(1) == 4 || f(2) == 4 || f(3) == 4
53
57
say 'success';
54
58
}
55
59
@@ -64,8 +68,8 @@ arguments into multiple calls to a function is called I<autothreading>.
64
68
A colon pair is a shorthand syntax used to create or visually present
65
69
a Pair object. The two most common forms are:
66
70
67
- :a(4) # Same as "a" => 4, same as Pair.new(:key<a>,:value(4) )
68
- :a<4> # Same as "a" => "4", same as Pair.new(:key<a>,:value<4> )
71
+ :a(4) # Same as "a" => 4, same as Pair.new("a",4 )
72
+ :a<4> # Same as "a" => "4", same as Pair.new("a","4" )
69
73
70
74
This is also known as the adverbial pair form. Note: when the part after
71
75
the colon and before the brackets is not a legal identifier, other
@@ -81,6 +85,15 @@ does not need commas, or even spaces:
81
85
82
86
:a(4):c:!d:c # Same as a => 4, c => True, d => False, c => True
83
87
88
+ Finally, if there is a variable with the same name as an intended adverbial
89
+ pair, you don't have to specify the name twice, but just specify the adverb
90
+ with the appropriate sigil:
91
+
92
+ :$foo # same as foo => $foo
93
+ :@bar # same as bar => @bar
94
+ :%mapper # same as mapper => %mapper
95
+ :&test # same as test => &test
96
+
84
97
= head1 Constraint
85
98
86
99
Constraints are a restriction placed on acceptable types for a parameter
@@ -89,11 +102,22 @@ following example, a constraint is used to make sure an error occurs if
89
102
anyone ever calls a subroutine named C < abbreviate > with a string which is
90
103
shorter than 10 characters:
91
104
92
- sub abbreviate (Str $thing where { .chars >= 10 }) { ... }
105
+ sub abbreviate(Str $thing where { .chars >= 10 }) { ... }
93
106
94
107
The C < Str > in the above example is also a constraint, but is usually
95
108
referred to as a "type constraint."
96
109
110
+ Note that you can elso differentiate candidates in a C < multi-dispatch >
111
+ by using a different constraint:
112
+
113
+ multi sub abbreviate(Str $thing where { .chars >= 10 }) {
114
+ "$thing.substr(0,10)..."
115
+ }
116
+ multi sub abbreviate(Str $thing) { $thing } # no constraint
117
+
118
+ say abbreviate("Worthington"; # Worthingto...
119
+ say abbreviate("Mäsak"); # Mäsak
120
+
97
121
= head1 Instance
98
122
99
123
An I < instance > of a class is also called an I < object > in some other
@@ -129,13 +153,17 @@ in Perl 6. It is what C<self> refers to in a method.
129
153
130
154
say 'str'.uc; # 'str' is the invocant of method uc
131
155
156
+ class A { method show { self } }
157
+ say A.new.show; # A.new
158
+
132
159
= head1 Literal
133
160
134
161
A I < literal > is a piece of code that directly stands for an (often built-in)
135
162
object and also refers to the object itself.
136
163
137
164
my $x = 2; # the 2 is a literal
138
165
say $x; # $x is not a literal, but a variable
166
+ my $s = "Foo"; # the "Foo" is a literal, the $s is a variable
139
167
140
168
= head1 lvalue
141
169
@@ -174,16 +202,26 @@ The C<mainline> is the program text that is not part of any kind of block.
174
202
}
175
203
f(); # in mainline again
176
204
205
+ You can also have the mainline of any package-like declarator, such as
206
+ C < class > , C < module > , C < grammar > , etc. These are typically run just after
207
+ the class/module/grammar have been compiled (or when loaded from a
208
+ pre-compiled file).
209
+
177
210
= head1 Slurpy
178
211
179
212
A parameter of a sub or method is said to be I < slurpy > if it can consume an
180
213
arbitrary number of arguments. It is indicated by an asterisk C < * > in front
181
214
of the parameter name.
182
215
183
- sub sum (*@numbers) {
216
+ sub sum(*@numbers) {
184
217
return [+] @numbers;
185
218
}
186
219
220
+ This can also be used to collect all possible named parameters in a call:
221
+
222
+ sub allnameds(*%named) { .say for %named.sort }
223
+ allnameds a => 42, :666b, :c<foo>; # a => 42, b => 666, c => foo
224
+
187
225
= head1 Type Object
188
226
189
227
A I < type object > is an object representing a class, role, package, grammar
0 commit comments