You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/syntax.pod
+46-31Lines changed: 46 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,8 +63,8 @@ A semicolon after the final statement (or after the final statement inside a
63
63
block) is optional, though it's good form to include it.
64
64
65
65
A closing curly brace followed by a newline character implies a statement
66
-
separator, which is why you don't need to write a semicolon before the last
67
-
line in the code
66
+
separator, which is why you don't need to write a semicolon after an C<if>
67
+
statement block.
68
68
69
69
=begincode
70
70
if True {
@@ -76,6 +76,13 @@ say "world";
76
76
Both semicolons are optional here, but leaving them out increases the chance
77
77
of syntax errors when adding more lines later.
78
78
79
+
You do need to include a semicolon between the C<if> block and the say statement if you want them all on one line.
80
+
81
+
=begincode
82
+
if True { say "Hello" }; say "world";
83
+
# ^^^ this ; is required
84
+
=endcode
85
+
79
86
=head2Comments
80
87
81
88
Comments are parts of the program text only intended for human readers, and
@@ -90,27 +97,31 @@ The most common form of comments in Perl 6 starts with a single hash character
90
97
C<#> and goes until the end of the line.
91
98
92
99
=begincode
93
-
94
100
if $age > 250 { # catch obvious outliers
95
101
# this is another comment!
96
102
die "That doesn't look right"
97
103
}
98
104
=endcode
99
105
100
-
=head3Embedded comments
106
+
=head3Multi-line / embedded comments
101
107
102
-
Embedded comments start with a hash character, followed by a backtick, and
103
-
then some opening bracketing character, and end with the matching closing
104
-
bracketing character.
108
+
Multi-line and embedded comments start with a hash character, followed by a
109
+
backtick, and then some opening bracketing character, and end with the matching
110
+
closing bracketing character. The content can not only span multiple lines, but
111
+
can also be embedded inline.
105
112
106
-
if #`( why would I ever write an inline comment here? ) True {
107
-
say "something stupid";
108
-
}
113
+
=begincode
114
+
if #`( why would I ever write an inline comment here? ) True {
115
+
say "something stupid";
116
+
}
117
+
=endcode
109
118
110
119
Brackets inside the comment can be nested, so in C<#`{ a { b } c }>, the
111
-
comment goes until the very end of the string.
120
+
comment goes until the very end of the string. You may also use more complex
121
+
brackets, such as C<#`{{ double-curly-brace }}>, which might help disambiguate
122
+
from nested brackets.
112
123
113
-
=head3Multiline comments
124
+
=head3Pod comments
114
125
115
126
Pod syntax can be used for multi-line comments
116
127
@@ -131,26 +142,27 @@ say 'code again';
131
142
=head2Identifiers
132
143
133
144
Identifiers are a grammatical building block that occur in several places. An
134
-
identifier is a primitive name, and must start with an alphabetic character
135
-
(or an underscore), followed by zero or more word characters (alphabetic,
136
-
underscore or letter). You can also join two or more identifiers by a dash
137
-
C<-> or a single quote C<'>, and the result is also considered to be an
138
-
identifier.
145
+
identifier is a primitive name, and must start with an alphabetic character (or
146
+
an underscore), followed by zero or more word characters (alphabetic,
147
+
underscore or number). You can also embed dashes C<-> or single quotes C<'> in
148
+
the middle, but not two in a row.
139
149
140
150
# valid identifiers:
141
151
x
142
152
something-longer
143
153
with-numbers1234
144
154
don't
155
+
fish-food
145
156
146
157
# not valid identifiers:
147
158
with-nubmers1234-5
148
159
42
149
160
is-prime?
161
+
fish--food
150
162
151
-
Names of constants, types and routines are identifiers, and they also appear
152
-
in variable names (optionally proceeded by a sigil; see
153
-
L<variables|/language/variables> for more details.)
163
+
Names of constants, types (including classes and modules) and routines (subs
164
+
and methods) are identifiers, and they also appear in variable names (usually
165
+
proceeded by a sigil; see L<variables|/language/variables> for more details.)
154
166
155
167
=head1Statements and Expressions
156
168
@@ -165,7 +177,7 @@ The C<do> prefix turns statements into expressions. So while
165
177
166
178
is an error,
167
179
168
-
my $x = do if True { 42; };
180
+
my $x = do if True { 42 };
169
181
170
182
assigns the return value of the if statement (here C<42>) to the variable
171
183
C<$x>.
@@ -209,14 +221,14 @@ variables:
209
221
say answer;
210
222
# ^^^^^^ constant
211
223
212
-
class Meta {
224
+
class Foo {
213
225
method type-name {
214
226
self.^name;
215
227
# ^^^^ built-in term 'self'
216
228
}
217
229
}
218
-
say Meta.type-name; # Meta
219
-
# ^^^^ type name
230
+
say Foo.type-name; # Foo
231
+
# ^^^ type name
220
232
221
233
222
234
=head2Literals
@@ -375,8 +387,6 @@ A L<Regex|/type/Regex> is declared with slashes like C</foo/>. Note that this C<
375
387
376
388
=head2Declarations
377
389
378
-
=commentTODO
379
-
380
390
=head3Variable declaration
381
391
382
392
my $x; # simple lexical variable
@@ -386,7 +396,9 @@ A L<Regex|/type/Regex> is declared with slashes like C</foo/>. Note that this C<
386
396
my Int $x where { $_ > 3 } = 7; # constrain the value based on a function
387
397
my Int $x where * > 3 = 7; # same constraint, but using L<Whatever> short-hand
388
398
389
-
See L<Variable Declarators and Scope|http://docs.perl6.org/language/variables#Variable_Declarators_and_Scope> for more details on other scopes (our, has).
0 commit comments