File Coverage

blib/lib/Scope/Escape.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Scope::Escape - reified escape continuations
4              
5             =head1 SYNOPSIS
6              
7             use Scope::Escape qw(current_escape_function);
8              
9             $escape = current_escape_function;
10             ...
11             $escape->($result);
12              
13             use Scope::Escape::Continuation
14             qw(current_escape_continuation);
15              
16             $escape = current_escape_continuation;
17             ...
18             $escape->go($result);
19              
20             =head1 DESCRIPTION
21              
22             This module provides a generalised facility for non-local control transfer
23             (jumping between stack frames), based on the well-thought-out semantics
24             of Common Lisp. It provides operators that will capture and reify the
25             escape (return) continuation of the current stack frame. The stack frame
26             can then be returned from, at (nearly) any time while it still exists,
27             via the reified continuation. This applies not only to subroutine stack
28             frames, but also to intermediate frames for code blocks, and other kinds
29             of stack frame. This facility can be used directly, or wrapped up to
30             build a more structured facility, as is done by L.
31              
32             The system of reified escape continuations is fundamentally different
33             from Perl's native C/C exception facility. With C,
34             the code initiating the non-local transfer has no control over where
35             it will go to. Each C frame gets to decide whether it wants
36             to act as the target of the thrown exception, but it must make this
37             decision based almost entirely on what was recorded in the exception
38             object, because the stack frames between the C and the C
39             have already been unwound by that time. With reified continuations,
40             however, the code initiating the transfer determines where it will go to
41             (by choosing which continuation to use), and that decision can be made
42             with all information about the circumstances still available.
43              
44             A reified escape continuation appears in Perl as a function object.
45             Calling the function results in returning from the stack frame that is the
46             target of the continuation. Values passed to the function are returned
47             from the target stack frame. Optionally, the continuation may be blessed
48             into the L class. This class provides a
49             method-based interface to the continuation: transferring through the
50             continuation, and querying its state, can be performed by method calls
51             on the continuation object. The methods can also be called directly,
52             as functions, on unblessed continuation functions.
53              
54             =head1 CONTINUATION TARGETS
55              
56             The operators supplied by this module generate continuations targeting
57             the "current scope". It is not always obvious what that is. Here are
58             the types of scope that occur in Perl and which can be escaped from by
59             means of the reified continuations supplied by this module:
60              
61             =over
62              
63             =item block
64              
65             Any braced block of code is a scope. Escaping from it jumps to the end
66             of the block. If the block is in a context where it supplies a value,
67             then using the escape continuation supplies that value, as if it had been
68             the value of the last statement executed in the block.
69              
70             In the case of the C block syntax, the value returned from the block
71             is used directly in the surrounding expression. Blocks in C,
72             C, and C also supply a value. Some other kinds of block
73             are mentioned specially below. In most other cases a block is in void
74             context.
75              
76             =item loop statement
77              
78             In a loop statement, the loop body is a block, with its own scope.
79             The C block, if any, is likewise a separate block scope. A loop
80             iteration is also a scope, and the test expression is evaluated within it,
81             so escaping from the test expression just skips to the next iteration.
82             None of these scopes return values.
83              
84             =item subroutine
85              
86             A subroutine call is a scope. It corresponds to the block scope of
87             the body of the subroutine. Escaping from that scope returns from the
88             subroutine. Values may be returned, depending on the context of the call.
89              
90             =item format
91              
92             A call to a format, via C, is a scope. The main activity of
93             a format is to output formatted text. Escaping early terminates the
94             outputting activity from the format, but page end processing still occurs
95             before C returns. No value is returned from the format.
96              
97             =item substitution
98              
99             The replacement part of a substitution (C) expression is evaluated
100             in its own scope. The scope supplies the (scalar) substring to be
101             inserted in place of what was matched.
102              
103             =item block eval
104              
105             The block form of C, used to catch C exceptions, provides
106             a scope, just like any other block. However, when the block returns
107             normally, C<$@> is cleared to indicate that there was no exception.
108             C is a type of expression, so the block commonly supplies a value.
109              
110             =item string eval
111              
112             The string form of C, used to parse code at runtime, provides a
113             scope in which the parsed code executes. In addition to parsing code
114             at runtime, this has the exception handling behaviour of block eval.
115             When the scope returns normally, C<$@> is cleared to indicate that there
116             was no exception. C is a type of expression, so the scope commonly
117             supplies a value.
118              
119             =item file
120              
121             If a file is parsed and executed, by C or C, the entire
122             file is a scope. Values may be returned, depending on the nature of
123             the calling site.
124              
125             =back
126              
127             These things are I scopes:
128              
129             =over
130              
131             =item conditional statement
132              
133             The test expression of a conditional statement executes in the scope
134             surrounding the conditional statement: there is no scope enclosing
135             just the conditional statement. The blocks that execute conditionally,
136             however, are each a scope, as normal for a code block.
137              
138             =item loop/conditional modifier
139              
140             Statements involving postfix modifiers for looping or conditionals do
141             not introduce any additional scopes. They are in this respect completely
142             unlike the loop and conditional statements where the keyword comes first.
143              
144             =back
145              
146             =head1 INACCESSIBLE STACK FRAMES
147              
148             Using Perl's native control constructs, an C block (or one of
149             its several equivalents) sets a limit on how far a non-local control
150             transfer can travel. Except when exiting the entire process, the only
151             way to non-locally transfer past the boundary of a single subroutine
152             call is C. An C block always stops the progress of a C,
153             and gives the catching code a choice about whether to set the C
154             going again through more stack frames. Some parts of Perl rely on
155             the result of this: that with an C frame it is impossible for a
156             non-local control transfer to pass one by.
157              
158             As a result of this, it is not possible, in the general case, to use an
159             escape continuation to cross over an C stack frame. These frames
160             are effectively impervious to non-local returns. This module currently
161             doesn't attempt to work around this limitation even in the cases where
162             it would have a fair chance of success. When there is an C frame
163             between the current code and the target of an escape continuation, the
164             target is said to be "inaccessible". The continuation remains valid when
165             this is the case, even though it will reject any attempt to actually
166             transfer through it. Once the last intervening C frame has been
167             exited, the target becomes accessible again, and the continuation can
168             be used normally. The details of this may change in the future, though
169             it is likely that there will always be some types of stack frame that
170             are impervious.
171              
172             =head1 CONTINUATION VALIDITY
173              
174             The continuations implemented by this module are not first-class.
175             That is, the existence of a continuation object does not keep its
176             target stack frame in existence. A continuation has a limited period of
177             validity, based on the treatment of its target, and so if a continuation
178             object is retained long enough it will refer to a continuation that is no
179             longer valid. Transfer through a continuation, and some other operations,
180             are not permitted when the continuation is invalid. This implemenatation
181             cannot always reliably detect that a continuation has become invalid,
182             so the prohibited operations invoke undefined behaviour.
183              
184             A continuation generally becomes invalid when its target stack frame is
185             unwound. The simplest case of this is when the target block completes
186             normal execution and returns normally. In that case, the continuation
187             becomes invalid as soon as the block has completed execution and unwinding
188             of the stack frame begins.
189              
190             When a non-local control transfer occurs (such as C, C,
191             or use of an escape continuation from this module), continuations
192             referencing stack frames higher than the target become invalid. They do
193             this as soon as the control transfer is initiated, before any of the stack
194             frames are actually unwound. However, if the non-local control transfer
195             is the use of an escape continuation, that continuation itself remains
196             valid during unwinding, until its target is unwound at the completion of
197             the control transfer. Thus cleanup code executed during unwinding can
198             itself perform non-local control transfers, provided that its target is
199             at least as low as the target of the current unwinding.
200              
201             If multiple continuations appear to target the same stack frame, such
202             as the frame established by a subroutine call, they are always actually
203             nested in some particular order. The earlier-established continuation
204             is always the outer one. Effectively, the remainder of a block is
205             nested inside the complete block. This corresponds to the way that
206             (both lexically and dynamically) things later in a block can shadow
207             things earlier in the block.
208              
209             Nominally, local returns from stack frames don't have the complications of
210             non-local control transfers. However, the way Perl performs them isn't
211             quite as local as it should be, in part because of the facility for a
212             block to set up several dynamic things in sequence. In continuation
213             terminology, reaching the end of the block acts much like a non-local
214             return to where the block was invoked, during which all of the block's
215             cleanup code will run in sequence. Continuations for those intermediate
216             scopes are all invalidated as soon as the interior of the block is
217             complete, rather than (as would be the case with a truly local return)
218             when the corresponding cleanup code runs. Also, the target continuation
219             of a normal Perl return is invalidated when the return commences,
220             so it is not valid to attempt a normal Perl return to the same target
221             during unwinding.
222              
223             =cut
224              
225             package Scope::Escape;
226              
227 19     19   517626 { use 5.008001; }
  19         68  
  19         1035  
228 19     19   110 use warnings;
  19         34  
  19         714  
229 19     19   118 use strict;
  19         50  
  19         1020  
230              
231             our $VERSION = "0.004";
232              
233 19     19   16693 use parent "Exporter";
  19         6790  
  19         98  
234             our @EXPORT_OK = qw(current_escape_function current_escape_continuation);
235              
236             require XSLoader;
237             XSLoader::load(__PACKAGE__, $VERSION);
238              
239             {
240             package Scope::Escape::Continuation;
241             our $VERSION = "0.004";
242             }
243              
244             =head1 OPERATORS
245              
246             These operators should be used through bareword function call syntax,
247             as if they were functions. However, they cannot otherwise be called as
248             functions in the normal manner. Attempting to take a reference to them
249             will result in a code reference that does not have any of the behaviour
250             described.
251              
252             =over
253              
254             =item current_escape_function
255              
256             Reifies the current scope's escape continuation, returning it as a
257             reference to an unblessed function. The function can be called through
258             this reference in order to return from the current scope. The function
259             can also be manually passed to the L methods.
260              
261             This operator is to be preferred if you want to treat the continuation
262             as a plain function. If access to the L
263             methods is a priority, prefer L.
264              
265             =item current_escape_continuation
266              
267             Reifies the current scope's escape continuation, returning it as a
268             reference to a L object. The methods of
269             that class can be called through it. The object can also be called as
270             a function in order to return from the current scope (the action of the
271             L method).
272              
273             This operator is to be preferred if you want to treat the continuation
274             as an opaque object and want to use the L
275             methods. If you want to treat the continuation as a plain function,
276             prefer L.
277              
278             =back
279              
280             =head1 SEE ALSO
281              
282             L,
283             L,
284             L,
285             L
286              
287             =head1 AUTHOR
288              
289             Andrew Main (Zefram)
290              
291             =head1 COPYRIGHT
292              
293             Copyright (C) 2010, 2011 Andrew Main (Zefram)
294              
295             =head1 LICENSE
296              
297             This module is free software; you can redistribute it and/or modify it
298             under the same terms as Perl itself.
299              
300             =cut
301              
302             1;