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
+38-10Lines changed: 38 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -345,24 +345,29 @@ arrays themselves:
345
345
=head3L<Hash|/type/Hash> literals
346
346
347
347
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
350
354
351
355
say {a => 73, foo => "fish"}.keys.join(" "); # a foo
352
356
# ^^^^^^^^^^^^^^^^^^^^^^^^ Hash constructor
353
357
354
-
Most often this is used with arrow pairs.
358
+
When assigning to a C<%> sigil variable, the curly braces are optional.
355
359
356
-
=head3L<Regex|/type/Regex> literals
360
+
my %ages = fred => 23, jean => 87, ann => 4;
357
361
358
-
=commentTODO
362
+
=head3L<Regex|/type/Regex> literals
359
363
360
364
A regex is declared with slashes like C</foo/>. Note that this C<//> syntax is shorthand for the full C<rx//> syntax.
361
365
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
363
369
364
-
my $pattern = /foo/;
365
-
say "Match!" if "asdfoosdf" ~~ $pattern; # Match!
370
+
my $r = /foo/; # Regexes can be assigned to variables
366
371
367
372
=head2Declarations
368
373
@@ -374,11 +379,15 @@ You can assign regexes to variables, just like with other literals.
374
379
my $x = 7; # initialize the variable
375
380
my Int $x = 7; # declare the type
376
381
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).
378
385
379
386
=head3Subroutine declaration
380
387
388
+
# The signature is optional
381
389
sub foo { say "Hello!" }
390
+
382
391
sub say-hello($to-whom) { say "Hello $to-whom!" }
383
392
384
393
You can also assign subroutines to variables.
@@ -387,14 +396,33 @@ You can also assign subroutines to variables.
387
396
my &f = -> { say "Hello!" } # Lambda style syntax. The & sigil indicates the variable holds a function
388
397
my $f = -> { say "Hello!" } # Functions can also be put into scalars
389
398
390
-
=head3Class, Role, and Grammar declaration
399
+
=head3Package, Class, Role, and Grammar declaration
400
+
401
+
package Gar { }
391
402
392
403
class Foo { }
393
404
394
405
role Bar { }
395
406
396
407
grammar Baz { }
397
408
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
+
=head3Multi-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.
0 commit comments