Skip to content

Commit 9726ca5

Browse files
author
Brock Wilcox
committed
Clarifications and improvements
1 parent ffa2e50 commit 9726ca5

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

doc/Language/syntax.pod

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ A semicolon after the final statement (or after the final statement inside a
6363
block) is optional, though it's good form to include it.
6464
6565
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.
6868
6969
=begin code
7070
if True {
@@ -76,6 +76,13 @@ say "world";
7676
Both semicolons are optional here, but leaving them out increases the chance
7777
of syntax errors when adding more lines later.
7878
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+
=begin code
82+
if True { say "Hello" }; say "world";
83+
# ^^^ this ; is required
84+
=end code
85+
7986
=head2 Comments
8087
8188
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
9097
C<#> and goes until the end of the line.
9198
9299
=begin code
93-
94100
if $age > 250 { # catch obvious outliers
95101
# this is another comment!
96102
die "That doesn't look right"
97103
}
98104
=end code
99105
100-
=head3 Embedded comments
106+
=head3 Multi-line / embedded comments
101107
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.
105112
106-
if #`( why would I ever write an inline comment here? ) True {
107-
say "something stupid";
108-
}
113+
=begin code
114+
if #`( why would I ever write an inline comment here? ) True {
115+
say "something stupid";
116+
}
117+
=end code
109118
110119
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.
112123
113-
=head3 Multiline comments
124+
=head3 Pod comments
114125
115126
Pod syntax can be used for multi-line comments
116127
@@ -131,26 +142,27 @@ say 'code again';
131142
=head2 Identifiers
132143
133144
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.
139149
140150
# valid identifiers:
141151
x
142152
something-longer
143153
with-numbers1234
144154
don't
155+
fish-food
145156
146157
# not valid identifiers:
147158
with-nubmers1234-5
148159
42
149160
is-prime?
161+
fish--food
150162
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.)
154166
155167
=head1 Statements and Expressions
156168
@@ -165,7 +177,7 @@ The C<do> prefix turns statements into expressions. So while
165177
166178
is an error,
167179
168-
my $x = do if True { 42; };
180+
my $x = do if True { 42 };
169181
170182
assigns the return value of the if statement (here C<42>) to the variable
171183
C<$x>.
@@ -209,14 +221,14 @@ variables:
209221
say answer;
210222
# ^^^^^^ constant
211223
212-
class Meta {
224+
class Foo {
213225
method type-name {
214226
self.^name;
215227
# ^^^^ built-in term 'self'
216228
}
217229
}
218-
say Meta.type-name; # Meta
219-
# ^^^^ type name
230+
say Foo.type-name; # Foo
231+
# ^^^ type name
220232
221233
222234
=head2 Literals
@@ -375,8 +387,6 @@ A L<Regex|/type/Regex> is declared with slashes like C</foo/>. Note that this C<
375387
376388
=head2 Declarations
377389
378-
=comment TODO
379-
380390
=head3 Variable declaration
381391
382392
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<
386396
my Int $x where { $_ > 3 } = 7; # constrain the value based on a function
387397
my Int $x where * > 3 = 7; # same constraint, but using L<Whatever> short-hand
388398
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).
399+
See L<Variable Declarators and
400+
Scope|http://docs.perl6.org/language/variables#Variable_Declarators_and_Scope>
401+
for more details on other scopes (our, has).
390402
391403
=head3 Subroutine declaration
392404
@@ -403,7 +415,8 @@ You can also assign subroutines to variables.
403415
404416
=head3 Module, Class, Role, and Grammar declaration
405417
406-
There are several types of compilation units (packages), each declared with a keyword, a name, some optional traits, and a body of functions.
418+
There are several types of compilation units (packages), each declared with a
419+
keyword, a name, some optional traits, and a body of functions.
407420
408421
module Gar { }
409422
@@ -461,10 +474,12 @@ There are five types (arrangements) for operators, each taking either one or two
461474
=head2 Meta Operators
462475
463476
Operators can be composed. A common example of this is combining an infix
464-
(binary) operator with assignment. You can do this with any binary operator.
477+
(binary) operator with assignment. You can combine assignment with any binary
478+
operator.
465479
466-
$x += 5 # Adds 5 to $x, same as $x = $x + 5
467-
$x min= 3 # Sets $x to the smaller of $x and 3, same as $x = $x min 3
480+
$x += 5 # Adds 5 to $x, same as $x = $x + 5
481+
$x min= 3 # Sets $x to the smaller of $x and 3, same as $x = $x min 3
482+
$x .= child # Equivalent to $x = $x.child
468483
469484
Wrap an infix operator in C<[ ]> to create a new reduction operator that works
470485
on a single list of inputs, resulting in a single value.

0 commit comments

Comments
 (0)