Skip to content

Commit 8058cb7

Browse files
committed
Merge pull request #189 from awwaiid/syntax-additions
Clarify Hash literal syntax. Expand on other things.
2 parents cecbf7a + 0046d89 commit 8058cb7

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

doc/Language/syntax.pod

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,24 +345,29 @@ arrays themselves:
345345
=head3 L<Hash|/type/Hash> literals
346346
347347
A pair of curly braces can surround a list of pairs to form a
348-
L<Hash|/type/Hash> literal; typically there is a comma-delimited list
349-
of pairs inside:
348+
L<Hash|/type/Hash> literal; typically there is a comma-delimited list of pairs
349+
inside. If a non-pair is used, it is assumed to be a key and the next element
350+
is the value. Most often this is used with simple arrow pairs.
351+
352+
say { a => 3, b => 23, :foo, :dog<cat>, "french", "fries" };
353+
# a => 3, b => 23, dog => cat, foo => True, french => fries
350354
351355
say {a => 73, foo => "fish"}.keys.join(" "); # a foo
352356
# ^^^^^^^^^^^^^^^^^^^^^^^^ Hash constructor
353357
354-
Most often this is used with arrow pairs.
358+
When assigning to a C<%> sigil variable, the curly braces are optional.
355359
356-
=head3 L<Regex|/type/Regex> literals
360+
my %ages = fred => 23, jean => 87, ann => 4;
357361
358-
=comment TODO
362+
=head3 L<Regex|/type/Regex> literals
359363
360364
A regex is declared with slashes like C</foo/>. Note that this C<//> syntax is shorthand for the full C<rx//> syntax.
361365
362-
You can assign regexes to variables, just like with other literals.
366+
/foo/ # Short version
367+
rx/foo/ # Longer version
368+
Q :regex /foo/ # Even longer version
363369
364-
my $pattern = /foo/;
365-
say "Match!" if "asdfoosdf" ~~ $pattern; # Match!
370+
my $r = /foo/; # Regexes can be assigned to variables
366371
367372
=head2 Declarations
368373
@@ -374,11 +379,15 @@ You can assign regexes to variables, just like with other literals.
374379
my $x = 7; # initialize the variable
375380
my Int $x = 7; # declare the type
376381
my Int $x:D = 7; # specify that the value must be defined (not undef)
377-
my Int $x where * > 3 = 7; # restrict the value based on a function
382+
my Int $x where * > 3 = 7; # constrain the value based on a function
383+
384+
See L<Variable Declarators and Scope|http://docs.perl6.org/language/variables#Variable_Declarators_and_Scope> for more details on other scopes (our, has).
378385
379386
=head3 Subroutine declaration
380387
388+
# The signature is optional
381389
sub foo { say "Hello!" }
390+
382391
sub say-hello($to-whom) { say "Hello $to-whom!" }
383392
384393
You can also assign subroutines to variables.
@@ -387,14 +396,33 @@ You can also assign subroutines to variables.
387396
my &f = -> { say "Hello!" } # Lambda style syntax. The & sigil indicates the variable holds a function
388397
my $f = -> { say "Hello!" } # Functions can also be put into scalars
389398
390-
=head3 Class, Role, and Grammar declaration
399+
=head3 Package, Class, Role, and Grammar declaration
400+
401+
package Gar { }
391402
392403
class Foo { }
393404
394405
role Bar { }
395406
396407
grammar Baz { }
397408
409+
You can declare a unit of things without explicit curly brackets.
410+
411+
unit package Gar;
412+
# ... stuff goes here instead of in {}'s
413+
414+
=head3 Multi-dispatch declaration
415+
416+
Subroutines can be declared with multiple signatures.
417+
418+
multi sub foo() { say "Hello!" }
419+
multi sub foo($name) { say "Hello $name!" }
420+
421+
Inside of a class, you can also declare multi-dispatch methods.
422+
423+
multi method greet { }
424+
multi method greet(Str $name) { }
425+
398426
=head2 L<Subroutine|/language/functions> calls
399427
400428
=comment TODO

0 commit comments

Comments
 (0)