Skip to content

Commit 8a4831a

Browse files
author
Brock Wilcox
committed
Start shuffling things into sections
1 parent 081331e commit 8a4831a

File tree

1 file changed

+94
-84
lines changed

1 file changed

+94
-84
lines changed

doc/Language/phasers.pod

Lines changed: 94 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ example, but run the statements as a whole at the indicated time:
6969
(Note, however, that the value of a variable calculated at compile time may
7070
not persist under run-time cloning of any surrounding closure.)
7171
72+
When multiple phasers are scheduled to run at the same moment, the general
73+
tiebreaking principle is that initializing phasers execute in order
74+
declared, while finalizing phasers execute in the opposite order, because
75+
setup and teardown usually want to happen in the opposite order from each
76+
other. When phasers are in different modules, the C<INIT> and C<END>
77+
phasers are treated as if declared at C<use> time in the using module. (It
78+
is erroneous to depend on this order if the module is used more than once,
79+
however, since the phasers are only installed the first time they're
80+
noticed.)
81+
7282
Most of the non-value-producing phasers may also be so used:
7383
7484
END say my $accumulator;
@@ -91,8 +101,6 @@ Code that is generated at run time can still fire off C<CHECK> and C<INIT>
91101
phasers, though of course those phasers can't do things that would require
92102
travel back in time. You need a wormhole for that.
93103
94-
The compiler is free to ignore C<LINK> phasers compiled at run time since
95-
they're too late for the application-wide linking decisions.
96104
97105
Some of these phasers also have corresponding traits that can be set on
98106
variables. These have the advantage of passing the variable in question
@@ -103,22 +111,26 @@ into the closure as its topic:
103111
Only phasers that can occur multiple times within a block are eligible for
104112
this per-variable form.
105113
106-
Apart from C<CATCH> and C<CONTROL>, which can only occur once, most of these
107-
can occur multiple times within the block. So they aren't really traits,
108-
exactly--they add themselves onto a list stored in the actual trait. So if
109-
you examine the C<ENTER> trait of a block, you'll find that it's really a
110-
list of phasers rather than a single phaser.
114+
The topic of the block outside a phaser is still available as C<<
115+
OUTER::<$_> >>. Whether the return value is modifiable may be a policy of
116+
the phaser in question. In particular, the return value should not be
117+
modified within a C<POST> phaser, but a C<LEAVE> phaser could be more
118+
liberal.
111119
112-
When multiple phasers are scheduled to run at the same moment, the general
113-
tiebreaking principle is that initializing phasers execute in order
114-
declared, while finalizing phasers execute in the opposite order, because
115-
setup and teardown usually want to happen in the opposite order from each
116-
other. When phasers are in different modules, the C<INIT> and C<END>
117-
phasers are treated as if declared at C<use> time in the using module. (It
118-
is erroneous to depend on this order if the module is used more than once,
119-
however, since the phasers are only installed the first time they're
120-
noticed.)
120+
Any phaser defined in the lexical scope of a method is a closure that closes
121+
over C<self> as well as normal lexicals. (Or equivalently, an
122+
implementation may simply turn all such phasers into submethods whose primed
123+
invocant is the current object.)
121124
125+
=head1 Program Execution Phasers
126+
=head2 BEGIN
127+
=head2 CHECK
128+
129+
=head2 LINK
130+
The compiler is free to ignore C<LINK> phasers compiled at run time since
131+
they're too late for the application-wide linking decisions.
132+
133+
=head2 INIT
122134
The semantics of C<INIT> and C<once> are not equivalent to each other in the
123135
case of cloned closures. An C<INIT> only runs once for all copies of a
124136
cloned closure. A C<once> runs separately for each clone, so separate
@@ -144,106 +156,104 @@ calls (assuming it wasn't called in sink context, in which case the C<once>
144156
is evaluated once only for its side effects). In particular, this means
145157
that C<once> can make use of any parameters passed in on the first call,
146158
whereas C<INIT> cannot.
159+
=head2 END
147160
148-
All of these phaser blocks can see any previously declared lexical
149-
variables, even if those variables have not been elaborated yet when the
150-
closure is invoked (in which case the variables evaluate to an undefined
151-
value.)
161+
=head1 Block Phasers
152162
153-
Note: Apocalypse 4 confused the notions of C<PRE>/C<POST> with
154-
C<ENTER>/C<LEAVE>. These are now separate notions. C<ENTER> and C<LEAVE>
155-
are used only for their side effects. C<PRE> and C<POST> return boolean
156-
values which, if false, trigger a runtime exception. C<KEEP> and C<UNDO>
157-
are just variants of C<LEAVE>, and for execution order are treated as part
158-
of the queue of C<LEAVE> phasers.
163+
Block-leaving phasers wait until the call stack is actually unwound to run.
164+
Unwinding happens only after some exception handler decides to handle the
165+
exception that way. That is, just because an exception is thrown past a stack
166+
frame does not mean we have officially left the block yet, since the exception
167+
might be resumable. In any case, exception handlers are specified to run within
168+
the dynamic scope of the failing code, whether or not the exception is
169+
resumable. The stack is unwound and the phasers are called only if an
170+
exception is not resumed.
159171
160-
It is conjectured that C<PRE> and C<POST> submethods in a class could be
161-
made to run as if they were phasers in any public method of the class. This
162-
feature is awaiting further exploration by means of a C<ClassHOW> extension.
172+
These
173+
can occur multiple times within the block. So they aren't really traits,
174+
exactly--they add themselves onto a list stored in the actual trait. So if
175+
you examine the C<ENTER> trait of a block, you'll find that it's really a
176+
list of phasers rather than a single phaser.
163177
164-
C<FIRST>, C<NEXT>, and C<LAST> are meaningful only within the lexical scope
165-
of a loop, and may occur only at the top level of such a loop block. A
166-
C<NEXT> executes only if the end of the loop block is reached normally, or
167-
an explicit C<next> is executed. In distinction to C<LEAVE> phasers, a
168-
C<NEXT> phaser is not executed if the loop block is exited via any exception
169-
other than the control exception thrown by C<next>. In particular, a
170-
C<last> bypasses evaluation of C<NEXT> phasers.
171178
172-
[Note: the name C<FIRST> used to be associated with C<state> declarations.
173-
Now it is associated only with loops. See the C<once> above for C<state>
174-
semantics.]
175179
176-
Except for C<CATCH> and C<CONTROL> phasers, which run while an exception is
177-
looking for a place to handle it, all block-leaving phasers wait until the
178-
call stack is actually unwound to run. Unwinding happens only after some
179-
exception handler decides to handle the exception that way. That is, just
180-
because an exception is thrown past a stack frame does not mean we have
181-
officially left the block yet, since the exception might be resumable. In
182-
any case, exception handlers are specified to run within the dynamic scope
183-
of the failing code, whether or not the exception is resumable. The stack
184-
is unwound and the phasers are called only if an exception is not resumed.
180+
All of these phaser blocks can see any previously declared lexical
181+
variables, even if those variables have not been elaborated yet when the
182+
closure is invoked (in which case the variables evaluate to an undefined
183+
value.)
184+
185+
=head2 ENTER
186+
An exception thrown from an C<ENTER> phaser will abort the C<ENTER> queue,
187+
but one thrown from a C<LEAVE> phaser will not.
188+
=head2 LEAVE
185189
186190
So C<LEAVE> phasers for a given block are necessarily evaluated after any
187191
C<CATCH> and C<CONTROL> phasers. This includes the C<LEAVE> variants,
188192
C<KEEP> and C<UNDO>. C<POST> phasers are evaluated after everything else,
189193
to guarantee that even C<LEAVE> phasers can't violate postconditions.
194+
195+
An exception thrown from an C<ENTER> phaser will abort the C<ENTER> queue,
196+
but one thrown from a C<LEAVE> phaser will not.
197+
198+
If a C<POST> fails or any kind of C<LEAVE> block throws an exception while
199+
the stack is unwinding, the unwinding continues and collects exceptions to
200+
be handled. When the unwinding is completed all new exceptions are thrown
201+
from that point.
202+
203+
=head2 KEEP
204+
For phasers such as C<KEEP> and C<POST> that are run when exiting a scope
205+
normally, the return value (if any) from that scope is available as the
206+
current topic within the phaser.
207+
=head2 UNDO
208+
=head2 PRE
190209
Likewise C<PRE> phasers fire off before any C<ENTER> or C<FIRST> (though not
191210
before C<BEGIN>, C<CHECK>, C<LINK>, or C<INIT>, since those are done at
192211
compile or process initialization time).
193212
213+
The exceptions thrown by
214+
failing C<PRE> and C<POST> phasers cannot be caught by a C<CATCH> in the
215+
same block, which implies that C<POST> phaser are not run if a C<PRE> phaser
216+
fails.
217+
218+
=head2 POST
219+
For phasers such as C<KEEP> and C<POST> that are run when exiting a scope
220+
normally, the return value (if any) from that scope is available as the
221+
current topic within the phaser.
222+
194223
The C<POST> block can be defined in one of two ways. Either the
195224
corresponding C<POST> is defined as a separate phaser, in which case C<PRE>
196225
and C<POST> share no lexical scope. Alternately, any C<PRE> phaser may
197226
define its corresponding C<POST> as an embedded phaser block that closes
198227
over the lexical scope of the C<PRE>.
199228
200-
If exit phasers are running as a result of a stack unwind initiated by an
201-
exception, this information needs to be made available. In any case, the
202-
information as to whether the block is being exited successfully or
203-
unsuccessfully needs to be available to decide whether to run C<KEEP> or
204-
C<UNDO> blocks (also see L</"Definition of Success">). How this information
205-
is made available is implementation dependent.
206-
207-
An exception thrown from an C<ENTER> phaser will abort the C<ENTER> queue,
208-
but one thrown from a C<LEAVE> phaser will not. The exceptions thrown by
209-
failing C<PRE> and C<POST> phasers cannot be caught by a C<CATCH> in the
210-
same block, which implies that C<POST> phaser are not run if a C<PRE> phaser
211-
fails.
212-
213229
If a C<POST> fails or any kind of C<LEAVE> block throws an exception while
214230
the stack is unwinding, the unwinding continues and collects exceptions to
215231
be handled. When the unwinding is completed all new exceptions are thrown
216232
from that point.
217233
218-
For phasers such as C<KEEP> and C<POST> that are run when exiting a scope
219-
normally, the return value (if any) from that scope is available as the
220-
current topic within the phaser.
234+
The exceptions thrown by
235+
failing C<PRE> and C<POST> phasers cannot be caught by a C<CATCH> in the
236+
same block, which implies that C<POST> phaser are not run if a C<PRE> phaser
237+
fails.
221238
222-
The topic of the block outside a phaser is still available as C<<
223-
OUTER::<$_> >>. Whether the return value is modifiable may be a policy of
224-
the phaser in question. In particular, the return value should not be
225-
modified within a C<POST> phaser, but a C<LEAVE> phaser could be more
226-
liberal.
239+
=head1 Loop Phasers
227240
228-
Any phaser defined in the lexical scope of a method is a closure that closes
229-
over C<self> as well as normal lexicals. (Or equivalently, an
230-
implementation may simply turn all such phasers into submethods whose primed
231-
invocant is the current object.)
241+
C<FIRST>, C<NEXT>, and C<LAST> are meaningful only within the lexical scope
242+
of a loop, and may occur only at the top level of such a loop block. A
243+
C<NEXT> executes only if the end of the loop block is reached normally, or
244+
an explicit C<next> is executed. In distinction to C<LEAVE> phasers, a
245+
C<NEXT> phaser is not executed if the loop block is exited via any exception
246+
other than the control exception thrown by C<next>. In particular, a
247+
C<last> bypasses evaluation of C<NEXT> phasers.
232248
233-
=head2 BEGIN
234-
=head2 CHECK
235-
=head2 LINK
236-
=head2 INIT
237-
=head2 END
238-
=head2 ENTER
239-
=head2 LEAVE
240-
=head2 KEEP
241-
=head2 UNDO
242249
=head2 FIRST
243250
=head2 NEXT
244251
=head2 LAST
245-
=head2 PRE
246-
=head2 POST
252+
253+
=head1 Exception Handling Phasers
247254
=head2 CATCH
248255
=head2 CONTROL
256+
257+
=head1 Object Phasers
249258
=head2 COMPOSE
259+

0 commit comments

Comments
 (0)