File Coverage

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