Skip to content

Commit 0333bf5

Browse files
committed
Document LEAVE + exit trap
Resolves #1581
1 parent 76badf6 commit 0333bf5

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

doc/Language/phasers.pod6

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ one thrown from a C<LEAVE> phaser will not.
263263
264264
=head2 X<LEAVE|Phasers>
265265
266-
Runs at every block exit time (even stack unwinds from exceptions).
266+
Runs at every block exit time (even stack unwinds from exceptions),
267+
except when the program exits abruptly (e.g. with
268+
L«C<exit>|/routine/exit»).
267269
268270
So C<LEAVE> phasers for a given block are necessarily evaluated after any
269271
C<CATCH> and C<CONTROL> phasers. This includes the C<LEAVE> variants, C<KEEP>

doc/Language/traps.pod6

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,40 @@ $a.sayit; # OUTPUT: «hi␤»
10361036
my $b = A.new;
10371037
$b.sayit; # nothing
10381038
1039-
=end pod
1039+
=head2 C<LEAVE> phaser and C<exit>
1040+
Using L«C<LEAVE>|/syntax/LEAVE» phaser to perform graceful resource
1041+
termination is a common pattern, but it does not cover the case when
1042+
the program is stopped with L«C<exit>|/routine/exit».
1043+
1044+
The following nondeterministic example should demonstrate the
1045+
complications of this trap:
1046+
=begin code :skip-test
1047+
my $x = say ‘Opened some resource’;
1048+
LEAVE say ‘Closing the resource gracefully’ with $x;
1049+
1050+
exit 42 if rand < ⅓; # ① 「exit」 is bad
1051+
die ‘Dying because of unhandled exception’ if rand < ½; # ② 「die」 is ok
1052+
# fallthru ③
1053+
=end code
10401054
1055+
There are three possible results:
1056+
=begin code :skip-test
1057+
1058+
Opened some resource
1059+
1060+
1061+
Opened some resource
1062+
Closing the resource gracefully
1063+
Dying because of unhandled exception
1064+
in block <unit> at print.p6 line 5
1065+
1066+
1067+
Opened some resource
1068+
Closing the resource gracefully
1069+
=end code
1070+
1071+
A call to C<exit> is part of normal operation for many programs, so
1072+
beware unintentional combination of C<LEAVE> phasers and C<exit> calls.
1073+
1074+
=end pod
10411075
# vim: expandtab shiftwidth=4 ft=perl6

xt/words.pws

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ eyewear
331331
fagner
332332
failover
333333
fallbacks
334+
fallthru
334335
false's
335336
falsy
336337
fatrat
@@ -675,6 +676,7 @@ nok
675676
nom
676677
nomatch
677678
nonchaining
679+
nondeterministic
678680
nonintuitive
679681
nonspacing
680682
nopackage

0 commit comments

Comments
 (0)