Skip to content

Commit a6612c2

Browse files
authored
Merge pull request #2804 from perl6/note-start-special-vars
Explain $! and $/ in start block
2 parents c076657 + 8aac66a commit a6612c2

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

doc/Language/control.pod6

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,30 @@ If the code inside the block has not finished, the call to C<.result> will
178178
wait until it is done.
179179
180180
A C<start> may also be used on a bare statement (without curly braces).
181-
This is mainly just useful when calling a subroutine / method on an object
181+
This is mainly useful when calling a subroutine / method on an object
182182
is the only thing to do asynchronously.
183183
184+
sub get42 { 42 }
185+
my $promise = start get42;
186+
say $promise.result; # OUTPUT: «42␤»
187+
188+
Note that code executed this way does not have access to the special
189+
variables L«C<$!>|/syntax/$!» and L«C<$/>|/syntax/$/» of its outer
190+
block, but receives new ones, so every asynchronous task has its
191+
per-task state.
192+
193+
Thus, C<try> expressions and regex matches executed in the
194+
asynchronous task have their per-task state.
195+
196+
'a' ~~ /a/; # $/ is set to 「a」
197+
try die; # $! is defined now with an anonymous AdHoc exception
198+
# as a code block
199+
await start { say $! }; # OUTPUT: «Nil␤»
200+
await start { say $/ }; # OUTPUT: «Nil␤»
201+
# as a single statement
202+
await start $!.say; # OUTPUT: «Nil␤»
203+
await start $/.say; # OUTPUT: «Nil␤»
204+
184205
=head1 X<if|control flow,if>
185206
186207
To conditionally run a block of code, use an C<if> followed by a condition.

0 commit comments

Comments
 (0)