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
+58-11Lines changed: 58 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -379,11 +379,12 @@ A L<Regex|/type/Regex> is declared with slashes like C</foo/>. Note that this C<
379
379
380
380
=head3Variable declaration
381
381
382
-
my $x; # simple lexical variable
383
-
my $x = 7; # initialize the variable
384
-
my Int $x = 7; # declare the type
385
-
my Int $x:D = 7; # specify that the value must be defined (not undef)
386
-
my Int $x where * > 3 = 7; # constrain the value based on a function
382
+
my $x; # simple lexical variable
383
+
my $x = 7; # initialize the variable
384
+
my Int $x = 7; # declare the type
385
+
my Int $x:D = 7; # specify that the value must be defined (not undef)
386
+
my Int $x where { $_ > 3 } = 7; # constrain the value based on a function
387
+
my Int $x where * > 3 = 7; # same constraint, but using L<Whatever> short-hand
387
388
388
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).
389
390
@@ -400,9 +401,11 @@ You can also assign subroutines to variables.
400
401
my &f = -> { say "Hello!" } # Lambda style syntax. The & sigil indicates the variable holds a function
401
402
my $f = -> { say "Hello!" } # Functions can also be put into scalars
402
403
403
-
=head3Package, Class, Role, and Grammar declaration
404
+
=head3Module, Class, Role, and Grammar declaration
404
405
405
-
package Gar { }
406
+
There are several types of compilation units (packages), each declared with a keyword, a name, some optional traits, and a body of functions.
407
+
408
+
module Gar { }
406
409
407
410
class Foo { }
408
411
@@ -412,11 +415,13 @@ You can also assign subroutines to variables.
412
415
413
416
You can declare a unit of things without explicit curly brackets.
414
417
415
-
unit package Gar;
418
+
unit module Gar;
416
419
# ... stuff goes here instead of in {}'s
417
420
418
421
=head3Multi-dispatch declaration
419
422
423
+
See also L<Multi-dispatch|http://docs.perl6.org/language/functions#Multi-dispatch>.
424
+
420
425
Subroutines can be declared with multiple signatures.
421
426
422
427
multi sub foo() { say "Hello!" }
@@ -427,18 +432,60 @@ Inside of a class, you can also declare multi-dispatch methods.
427
432
multi method greet { }
428
433
multi method greet(Str $name) { }
429
434
430
-
=head2Subroutine calls
435
+
=head1Subroutine calls
431
436
432
437
See L<functions|/language/functions>.
433
438
434
439
=commentTODO
435
440
436
441
foo; # Invoke the function foo with no arguments
437
442
foo(); # Invoke the function foo with no arguments
438
-
$f(); # Invoke $f, which contains a function
443
+
&f(); # Invoke &f, which contains a function
439
444
440
445
=head1Operators
441
446
442
-
See L<Operators|/language/operators>.
447
+
See L<Operators|/language/operators> for lots of details.
448
+
449
+
Operators are functions with a more symbol heavy and composable syntax. Like
450
+
other functions, operators can be multi-dispatch to allow for context-specific
451
+
usage.
452
+
453
+
There are five types (arrangements) for operators, each taking either one or two arguments.
454
+
455
+
++$x # prefix, operator comes before single input
456
+
5 + 3 # infix, operator is between two inputs
457
+
$x++ # postfix, operator is after single input
458
+
<the blue sky> # circumfix, operator surrounds single input
459
+
%foo<bar> # postcircumfix, operator comes after first input and surrounds second
460
+
461
+
=head2Meta Operators
462
+
463
+
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.
465
+
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
468
+
469
+
Wrap an infix operator in C<[ ]> to create a new reduction operator that works
470
+
on a single list of inputs, resulting in a single value.
0 commit comments