/
glossary.pod
151 lines (106 loc) · 4.19 KB
/
glossary.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
=begin pod
=TITLE Glossary of Perl 6 terminology
=head1 Anonymous
A subroutine, method or submethod is called I<anonymous> if it can't
be called by name.
# named subroutine
sub double($x) { 2 * $x };
# anonymous subroutine, stored in a named scalar
my $double = sub ($x) { 2 * $x };
Note that it is still allowed to have a name
# anonymous, but knows its own name
my $s = anon sub triple($x) { 3 * $x }
say $s.name; # triple
=head1 Autothreading
Autothreading is what happens if you pass a junction to a sub that
expects a parameter of type C<Any> or a subtype thereof. The call is
executed multiple times, each time with a different eigenstate of the
junction. The result of these calls is assembled in a junction of the
same type as the original junction.
sub f($x) { 2 * $x };
if f(1|2|3) == 4 {
say 'success';
}
Here C<f()> is a sub with one parameter, and since it has no explicit
type, it is implicitly typed as C<Any>. The C<Junction> argument causes
the C<f(1|2|3)> call to be internally executed as C<f(1)|f(2)|f(3)>,
and the resulting junction is C<2|4|6>. This process of separating
Junction arguments into multiple calls to a function is called
I<autothreading>.
=head1 Instance
An I<instance> of a class is also called an I<object> in some other
programming languages. It has a storage for attributes, and is often
the return value of a call to a method called C<new>, or a literal.
Instances of most types are defined to be C<True>
i.e., C<defined($instance)> is C<True>.
my Str $str = "hello"; ## this is with builtin types, e.g. Str
if defined($str) {
say "Oh, yeah. I'm defined.";
} else {
say "No. Something off? ";
}
## if you wanted objects...
class A {
# nothing here for now.
}
my $an_instance = A.new;
say $an_instance.defined.perl;# defined($an_instance) works too.
Or to put it another way, a class has all the blueprint of methods and
attributes, and an instance carries it forward into the real world.
=head1 Invocant
The object on which a method is called is called the I<invocant> in Perl 6.
It is what C<self> refers to in a method.
say 'str'.uc; # 'str' is the invocant of method uc
=head1 Literal
A I<literal> is a piece of code that directly stands for a (often
built-in) object, and also refers to the object itself.
my $x = 2; # the 2 is a literal
say $x; # $x is not a literal, but a variable
=head1 lvalue
An I<lvalue> or a I<left value> is anything that can appear on the left
hand side of the assignment operator C<=>; anything you can assign
to.
Typical lvalues are variables, private and C<is rw> attributes, lists
of variables and lvalue subroutines.
Examples of lvalues:
Declaration lvalue Comments
my $x; $x
my ($a, $b); ($a, $b)
has $!attribute; $!attribute Only inside classes
has $.attrib is rw; $.attrib
sub a is rw { $x }; a()
Examples of things that are not lvalues
3 # literals
constant x = 3; # constants
has $.attrib; # attributes; you can only assign to $!attrib
sub f { }; f(); # "normal" subs are not writable
sub f($x) { $x = 3 }; # error - parameters are read-only by default
=head1 Mainline
The C<mainline> is the program text that is not part of any kind of block.
use v6; # mainline
sub f {
# not in mainline, in sub f
}
f(); # in mainline again
=head1 Slurpy
A parameter of a sub or method is said to be I<slurpy> if it can
consume an arbitrary number of arguments. It is indicated by an
asterisk C<*> in front of the parameter name.
sub sum (*@numbers) {
return [+] @numbers;
}
=head1 Type Object
A I<type object> is an object representing a class, role, package,
grammar or enum. It is generally accessible with the same name as
the type.
class A { };
say A; # A is the type object
my $x = A.new(); # same here
my $x = class {
method greet() {
say "hi";
}
}
# $x now holds a type object returned from the
# anonymous class definition
=end pod