File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff 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
268270So C < LEAVE > phasers for a given block are necessarily evaluated after any
269271C < CATCH > and C < CONTROL > phasers. This includes the C < LEAVE > variants, C < KEEP >
Original file line number Diff line number Diff line change @@ -1036,6 +1036,40 @@ $a.sayit; # OUTPUT: «hi»
10361036my $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
Original file line number Diff line number Diff line change @@ -331,6 +331,7 @@ eyewear
331331fagner
332332failover
333333fallbacks
334+ fallthru
334335false's
335336falsy
336337fatrat
675676nom
676677nomatch
677678nonchaining
679+ nondeterministic
678680nonintuitive
679681nonspacing
680682nopackage
You can’t perform that action at this time.
0 commit comments