@@ -20,7 +20,7 @@ types of arguments, and the return value.
20
20
21
21
Introspection on subroutines is provided via L < C < Routine > |/type/Routine> .
22
22
23
- = head1 Defining/Creating/Using Functions
23
+ = head1 Defining/Creating/Using functions
24
24
25
25
= head2 X < Subroutines|declarator,sub >
26
26
@@ -100,7 +100,7 @@ Or even
100
100
say { $^a ** 2 + $^b ** 2 }(3, 4) # OUTPUT: «25»
101
101
102
102
103
- = head2 X « Blocks and Lambdas |syntax,-> »
103
+ = head2 X « Blocks and lambdas |syntax,-> »
104
104
105
105
Whenever you see something like C « { $_ + 42 } » ,
106
106
C « -> $a, $b { $a ** $b } » , or C « { $^text.indent($:spaces) } » , that's
@@ -128,7 +128,7 @@ sub formatB<(Str $s)> { ... }
128
128
Details about the syntax and use of signatures can be found in the
129
129
L < documentation on the C < Signature > class|Signature > .
130
130
131
- = head3 Automatic Signatures
131
+ = head3 Automatic signatures
132
132
133
133
X < |@_ > X < |%_ >
134
134
If no signature is provided but either of the two automatic variables C < @_ > or
@@ -202,7 +202,7 @@ values.
202
202
put b.perl;
203
203
# OUTPUT: «\("a", "b", "c")»
204
204
205
- = head2 Return Type Constraints
205
+ = head2 Return type constraints
206
206
207
207
Perl 6 has many ways to specify a function's return type:
208
208
@@ -427,13 +427,13 @@ at /tmp/hDM1N2OAOo:1
427
427
Anonymous sub cannot be declared C < only > . C < only sub {}' > will throw an error of
428
428
type, surprisingly, C < X::Anon::Multi > .
429
429
430
- = head1 Conventions and Idioms
430
+ = head1 Conventions and idioms
431
431
432
432
While the dispatch system described above provides a lot of flexibility,
433
433
there are some conventions that most internal functions, and those in
434
434
many modules, will follow.
435
435
436
- = head2 Slurpy Conventions
436
+ = head2 Slurpy conventions
437
437
438
438
Perhaps the most important one of these conventions is the way slurpy list
439
439
arguments are handled. Most of the time, functions will not automatically
@@ -490,7 +490,7 @@ grab($b); # OUTPUT: «grab 1grab 2»
490
490
my \c = (1, 2); # Sigilless variables always bind, even with '='
491
491
grab(c); # OUTPUT: «grab 1grab 2»
492
492
493
- = head1 Functions are First-Class Objects
493
+ = head1 Functions are first-class objects
494
494
495
495
Functions and other code objects can be passed around as values, just like any
496
496
other object.
@@ -519,7 +519,7 @@ which applies a function to each input element:
519
519
my @squared = map &square, 1..5;
520
520
say join ', ', @squared; # OUTPUT: «1, 4, 9, 16, 25»
521
521
522
- = head2 Z <> Infix Form
522
+ = head2 Z <> Infix form
523
523
524
524
To call a subroutine with 2 arguments like an infix operator, use a subroutine
525
525
reference surrounded by C < [ > and C < ] > .
@@ -528,6 +528,7 @@ reference surrounded by C<[> and C<]>.
528
528
say 21 [&plus] 21;
529
529
# OUTPUT: «42»
530
530
531
+ X < |closures >
531
532
= head2 Closures
532
533
533
534
All code objects in Perl 6 are I < closures > , which means they can reference
@@ -542,12 +543,12 @@ lexical variables from an outer scope.
542
543
$generated(); # OUTPUT: «42»
543
544
544
545
Here, C < $y > is a lexical variable inside C < generate-sub > , and the inner
545
- subroutine that is returned uses it. By the time that the inner sub is called,
546
+ subroutine that is returned uses it. By the time that inner sub is called,
546
547
C < generate-sub > has already exited. Yet the inner sub can still use C < $y > ,
547
548
because it I < closed > over the variable.
548
549
549
- Another closure example is the use of L < map|/type/List#routine_map >
550
- to multiply a list of numbers:
550
+ Another closure example is the use of L < map|/type/List#routine_map > to multiply
551
+ a list of numbers:
551
552
552
553
my $multiply-by = 5;
553
554
say join ', ', map { $_ * $multiply-by }, 1..5; # OUTPUT: «5, 10, 15, 20, 25»
@@ -614,7 +615,7 @@ pragma C<use soft;> to prevent inlining to allow wrapping at runtime.
614
615
615
616
= comment Important ones: candidates, wrap, unwrap, assuming, arity, count
616
617
617
- = head1 Defining Operators
618
+ = head1 Defining operators
618
619
619
620
Operators are just subroutines with funny names. The funny names are composed
620
621
of the category name (C < infix > , C < prefix > , C < postfix > , C < circumfix > ,
@@ -964,9 +965,8 @@ multi a(Int $x) {
964
965
say (a 10); # OUTPUT: «36288002»
965
966
= end code
966
967
967
- = head2 sub nextcallee
968
-
969
968
X < |dispatch,nextcallee >
969
+ = head2 sub nextcallee
970
970
971
971
Redispatch may be required to call a block that is not the current scope what
972
972
provides C < nextsame > and friends with the problem to referring to the wrong
@@ -997,9 +997,9 @@ executed in parallel, after some timeout, and then returns. We can't use
997
997
C < nextsame > here, because it'd be trying to C < nextsame > the Promise's block
998
998
instead of our original routine.
999
999
1000
- = head2 wrapped routines
1001
-
1002
1000
X < |dispatch,wrapped routines >
1001
+ = head2 Wrapped routines
1002
+
1003
1003
1004
1004
Besides those are mentioned above, re-dispatch is helpful in more situations.
1005
1005
One is for dispatching to wrapped routines:
@@ -1020,7 +1020,7 @@ say square-root(4); # OUTPUT: «2»
1020
1020
say square-root(-4); # OUTPUT: «0+2i»
1021
1021
= end code
1022
1022
1023
- = head2 routines of parent class
1023
+ = head2 Routines of parent class
1024
1024
1025
1025
Another use case is to re-dispatch to methods from parent classes.
1026
1026
@@ -1035,7 +1035,7 @@ class LoggedVersion is Version {
1035
1035
say LoggedVersion.new('1.0.2');
1036
1036
= end code
1037
1037
1038
- = head1 Coercion Types
1038
+ = head1 Coercion types
1039
1039
1040
1040
Coercion types force a specific type for routine arguments while allowing
1041
1041
the routine itself to accept a wider input. When invoked, the arguments are
@@ -1112,16 +1112,15 @@ for (2,4) X (1,2) -> ($a,$b) {
1112
1112
In this case, we are coercing an C < Int > to a C < Bool > , which is then printed (put
1113
1113
into a string context) in the C < for > loop that calls the function.
1114
1114
1115
- = head1 sub MAIN
1116
-
1117
1115
X < |MAIN > X < |command line arguments >
1116
+ = head1 sub MAIN
1118
1117
1119
- The sub with the special name C < MAIN > is executed after all relevant phasers. Its signature is the means by which command line arguments can be
1120
- parsed. Multi methods are supported and a usage method is automatically
1121
- generated and displayed if no command line arguments are provided. All command
1122
- line arguments are also available in
1123
- L < C < @*ARGS > |/language/variables#Dynamic_variables> , which can be mutated before
1124
- being processed by C < MAIN > .
1118
+ The sub with the special name C < MAIN > is executed after all relevant phasers.
1119
+ Its signature is the means by which command line arguments can be parsed. Multi
1120
+ methods are supported and a usage method is automatically generated and
1121
+ displayed if no command line arguments are provided. All command line arguments
1122
+ are also available in L < C < @*ARGS > |/language/variables#Dynamic_variables> , which
1123
+ can be mutated before being processed by C < MAIN > .
1125
1124
1126
1125
Unlike other ordinary functions, any return value provided by C < MAIN > will be
1127
1126
ignored by the invoker, even if explicitly set by means of a C < return > statement
@@ -1213,7 +1212,7 @@ unit sub MAIN( Int :$length = 24,
1213
1212
Note that this is only appropriate if you do not need a C < proto > or C < multi > definition.
1214
1213
1215
1214
X < |USAGE > X < |$*USAGE >
1216
- = head1 sub USAGE
1215
+ = head1 sub C < USAGE >
1217
1216
1218
1217
If no multi candidate of C < MAIN > is found for the given command line
1219
1218
parameters, the sub C < USAGE > is called. If no such method is found,
0 commit comments