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
say "Result2 is { $result2.^name }"; # OUTPUT: «Result2 is Any»
77
80
=endcode
78
81
79
-
A L<C<Match> will be C<Nil>|https://docs.perl6.org/language/regexes#Literals> if it finds nothing; however it assigning C<Nil> to C<$result2> above will result in its default value, which is C<Any> as shown.
82
+
A L<C<Match> will be C<Nil>|https://docs.perl6.org/language/regexes#Literals>
83
+
if it finds nothing; however it assigning C<Nil> to C<$result2> above
84
+
will result in its default value, which is C<Any> as shown.
80
85
81
86
=head2Using a block to interpolate anon state vars
82
87
83
-
The programmer intended for the code to count the number of times the routine
84
-
is called, but the counter is not increasing:
88
+
The programmer intended for the code to count the number of times the
89
+
routine is called, but the counter is not increasing:
85
90
86
91
=begincode
87
92
sub count-it { say "Count is {$++}" }
@@ -94,7 +99,7 @@ is called, but the counter is not increasing:
94
99
=endcode
95
100
96
101
When it comes to state variables, the block in which the vars are
97
-
declared gets cloned —and vars get initialized anew—whenever that
102
+
declared gets cloned —and vars get initialized anew—whenever that
98
103
block's block is re-entered. This lets constructs like the one below
99
104
behave appropriately: the state variable inside the loop gets
100
105
initialized anew each time the sub is called:
@@ -151,8 +156,8 @@ Alternatively, you can also use the L<concatenation operator|/routine/~> instead
151
156
152
157
=head2Beware of empty "blocks"
153
158
154
-
Curly braces are used to declare blocks. However, empty curly braces will
155
-
declare a hash.
159
+
Curly braces are used to declare blocks. However, empty curly braces
160
+
will declare a hash.
156
161
157
162
=begincode
158
163
$ = {say 42;} # Block
@@ -161,7 +166,8 @@ $ = {…} # Block
161
166
$ = { } # Hash
162
167
=endcode
163
168
164
-
You can use the second form if you effectively want to declare an empty block:
169
+
You can use the second form if you effectively want to declare an empty
170
+
block:
165
171
166
172
my &does-nothing = {;};
167
173
say does-nothing(33);# OUTPUT: «Nil»
@@ -171,9 +177,9 @@ You can use the second form if you effectively want to declare an empty block:
171
177
172
178
=head2Assigning to attributes
173
179
174
-
Newcomers often think that, because attributes with accessors are declared
175
-
as C<has $.x>, they can assign to C<$.x> inside the class. That's not the
176
-
case.
180
+
Newcomers often think that, because attributes with accessors are
181
+
declared as C<has $.x>, they can assign to C<$.x> inside the class.
182
+
That's not the case.
177
183
178
184
For example
179
185
@@ -229,8 +235,8 @@ class A {
229
235
say A.new(x => 42).x; # OUTPUT: «Any»
230
236
=endcode
231
237
232
-
leaves C<$!x> uninitialized, because the custom C<BUILD> doesn't initialize
233
-
it.
238
+
leaves C<$!x> uninitialized, because the custom C<BUILD> doesn't
239
+
initialize it.
234
240
235
241
B<Note:> Consider using L<TWEAK|/language/objects#index-entry-TWEAK>
0 commit comments