line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Scope::Escape::Sugar - whizzy syntax for non-local control transfer |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Scope::Escape::Sugar |
8
|
|
|
|
|
|
|
qw(with_escape_function with_escape_continuation); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
{ with_escape_function $e; ...; $e->($r); ...; } |
11
|
|
|
|
|
|
|
with_escape_function $e { ...; $e->($r); ...; } |
12
|
|
|
|
|
|
|
$res = with_escape_function($e { ...; $e->($r); ...; }); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
{ with_escape_continuation $e; ...; $e->($r); ...; } |
15
|
|
|
|
|
|
|
with_escape_continuation $e { ...; $e->($r); ...; } |
16
|
|
|
|
|
|
|
$res = with_escape_continuation($e { ...; $e->($r); ...; }); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Scope::Escape::Sugar qw(block return_from); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
{ block foo; ...; return_from foo $r; ...; } |
21
|
|
|
|
|
|
|
block foo { ...; return_from foo $r; ...; } |
22
|
|
|
|
|
|
|
$res = block(foo { ...; return_from foo $r; ...; }); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Scope::Escape::Sugar qw(catch throw); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
{ catch "foo"; ...; } |
27
|
|
|
|
|
|
|
catch "foo" { ...; } |
28
|
|
|
|
|
|
|
$res = catch("foo" { ...; }); |
29
|
|
|
|
|
|
|
throw("foo", $r); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
This module provides specialised syntax for non-local control transfer |
34
|
|
|
|
|
|
|
(jumping between stack frames), mainly based on the operators in Common |
35
|
|
|
|
|
|
|
Lisp. The non-local control transfers behave exactly like those of |
36
|
|
|
|
|
|
|
L, which should be consulted for the semantic details. |
37
|
|
|
|
|
|
|
This module provides more structured facilities, which take a variety |
38
|
|
|
|
|
|
|
of approaches to referencing the stack frame to be transferred to. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
package Scope::Escape::Sugar; |
43
|
|
|
|
|
|
|
|
44
|
3
|
|
|
3
|
|
68238
|
{ use 5.011002; } |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
107
|
|
45
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
80
|
|
46
|
3
|
|
|
3
|
|
14
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
109
|
|
47
|
|
|
|
|
|
|
|
48
|
3
|
|
|
3
|
|
2682
|
use B::Hooks::EndOfScope 0.05 (); |
|
3
|
|
|
|
|
45743
|
|
|
3
|
|
|
|
|
102
|
|
49
|
3
|
|
|
3
|
|
3321
|
use Devel::CallChecker 0.003 (); |
|
3
|
|
|
|
|
6381
|
|
|
3
|
|
|
|
|
92
|
|
50
|
3
|
|
|
3
|
|
2684
|
use Devel::CallParser 0.000 (); |
|
3
|
|
|
|
|
3084
|
|
|
3
|
|
|
|
|
99
|
|
51
|
3
|
|
|
3
|
|
5005
|
use Scope::Escape 0.004 (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
our $VERSION = "0.002"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use parent "Exporter"; |
56
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
57
|
|
|
|
|
|
|
with_escape_function with_escape_continuation |
58
|
|
|
|
|
|
|
block return_from |
59
|
|
|
|
|
|
|
catch throw |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
require XSLoader; |
63
|
|
|
|
|
|
|
XSLoader::load(__PACKAGE__, $VERSION); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 OPERATORS AND FUNCTIONS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The items shown here are mostly not ordinary functions. Most are operators |
68
|
|
|
|
|
|
|
that introduce a form that has some special syntax, not conforming |
69
|
|
|
|
|
|
|
to the ordinary Perl syntax. The documentation shows the complete |
70
|
|
|
|
|
|
|
syntax of forms that use the operator. The complete form may be |
71
|
|
|
|
|
|
|
either a statement or an expression, as indicated in the documentation. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 Direct escape continuation access |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
This facility provides direct access to the continuation functions/objects |
76
|
|
|
|
|
|
|
implemented by L, referenced through lexically-scoped |
77
|
|
|
|
|
|
|
variables. It is just slightly more structured than direct use of |
78
|
|
|
|
|
|
|
L's operators. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
In each version, there is a code block and a variable I. |
81
|
|
|
|
|
|
|
I is a lexically-scoped (C-like) scalar variable. |
82
|
|
|
|
|
|
|
Its name must start with a C<$> sigil, and must not include package |
83
|
|
|
|
|
|
|
qualification. I will be defined lexically, being visible |
84
|
|
|
|
|
|
|
in the code textually contained within the block. Its value will be a |
85
|
|
|
|
|
|
|
reference to an escape continuation targetting the block. Calling the |
86
|
|
|
|
|
|
|
continuation as a function will result in the code block exiting, |
87
|
|
|
|
|
|
|
returning the values that were passed to the continuation. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Do not assign a new value to I. In this version of this module, |
90
|
|
|
|
|
|
|
I behaves like a normal writable variable, but this is an |
91
|
|
|
|
|
|
|
implementation accident and may change in a future version. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=over |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item with_escape_function ESCAPE_VAR; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
This form is a complete statement, ending with semicolon. |
98
|
|
|
|
|
|
|
I is lexically defined within the enclosing block (from |
99
|
|
|
|
|
|
|
this statement to the end of the block), and contains a reference to an |
100
|
|
|
|
|
|
|
unblessed escape function for the enclosing block. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item with_escape_function ESCAPE_VAR BLOCK |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This form is a complete statement, ending with the closing brace of |
105
|
|
|
|
|
|
|
I. I is executed normally. |
106
|
|
|
|
|
|
|
I is lexically defined within I, and contains a |
107
|
|
|
|
|
|
|
reference to an unblessed escape function for the I. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item with_escape_function(ESCAPE_VAR BLOCK) |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This form is an expression. I is executed normally, and its |
112
|
|
|
|
|
|
|
return value will become the value of this expression. |
113
|
|
|
|
|
|
|
I is lexically defined within I, and contains a |
114
|
|
|
|
|
|
|
reference to an unblessed escape function for the I. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item with_escape_continuation ESCAPE_VAR; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This form is a complete statement, ending with semicolon. |
119
|
|
|
|
|
|
|
I is lexically defined within the enclosing block (from |
120
|
|
|
|
|
|
|
this statement to the end of the block), and contains a reference to a |
121
|
|
|
|
|
|
|
blessed escape continuation for the enclosing block. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item with_escape_continuation ESCAPE_VAR BLOCK |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This form is a complete statement, ending with the closing brace of |
126
|
|
|
|
|
|
|
I. I is executed normally. |
127
|
|
|
|
|
|
|
I is lexically defined within I, and contains a |
128
|
|
|
|
|
|
|
reference to a blessed escape continuation for the I. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item with_escape_continuation(ESCAPE_VAR BLOCK) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
This form is an expression. I is executed normally, and its |
133
|
|
|
|
|
|
|
return value will become the value of this expression. |
134
|
|
|
|
|
|
|
I is lexically defined within I, and contains a |
135
|
|
|
|
|
|
|
reference to a blessed escape continuation for the I. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=back |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 Returnable blocks with lexically-scoped names |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
This facility provides lexical scoping of names for non-locally |
142
|
|
|
|
|
|
|
returnable blocks, while avoiding visible reification of continuations. |
143
|
|
|
|
|
|
|
(C and C provide the |
144
|
|
|
|
|
|
|
same lexical scoping, but reify the continuations and put the lexical |
145
|
|
|
|
|
|
|
names in the ordinary scalar variable namespace.) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
In each version, there is a code block which is labelled with a static |
148
|
|
|
|
|
|
|
bareword identifier I. The tag is lexically scoped, being visible |
149
|
|
|
|
|
|
|
in the code textually contained within the block. The C |
150
|
|
|
|
|
|
|
operator can then be used to return from the textually enclosing block |
151
|
|
|
|
|
|
|
with a specified tag. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
In Common Lisp (the model for these special forms), there are many |
154
|
|
|
|
|
|
|
implicit returnable blocks, in addition to the explicit ones established |
155
|
|
|
|
|
|
|
by the C operator. Most notably, the body of each C-defined |
156
|
|
|
|
|
|
|
function is a returnable block tagged with the function's name. |
157
|
|
|
|
|
|
|
This module does not perceive any such implicit blocks: a C |
158
|
|
|
|
|
|
|
form will only return from a block explicitly established with C. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item block TAG; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This form is a complete statement, ending with semicolon. |
165
|
|
|
|
|
|
|
The enclosing block (from this statement to the end of the block) is |
166
|
|
|
|
|
|
|
returnable, tagged with I. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item block TAG BLOCK |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This form is a complete statement, ending with the closing brace of |
171
|
|
|
|
|
|
|
I. I is executed normally. |
172
|
|
|
|
|
|
|
I is returnable, tagged with I. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item block(TAG BLOCK) |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This form is an expression. I is executed normally, and its |
177
|
|
|
|
|
|
|
return value will become the value of this expression. |
178
|
|
|
|
|
|
|
I is returnable, tagged with I. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item return_from TAG VALUE ... |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item return_from(TAG VALUE ...) |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This form is an expression. It transfers control to exit from the |
185
|
|
|
|
|
|
|
lexically enclosing returnable block tagged with I. If there is |
186
|
|
|
|
|
|
|
no matching block, it is a compile-time error. Zero or more Is |
187
|
|
|
|
|
|
|
may be supplied, which determine what will be returned from the block. |
188
|
|
|
|
|
|
|
(Each I is stated as an expression, which is evaluated normally.) |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
The Is are interpreted according to the syntactic context in |
191
|
|
|
|
|
|
|
which the target block was invoked. In void context, all the Is |
192
|
|
|
|
|
|
|
are ignored. In scalar context, only the last I is returned, |
193
|
|
|
|
|
|
|
or C if no Is were supplied. In list context, the full |
194
|
|
|
|
|
|
|
list of Is is used unmodified. Note that this non-local context |
195
|
|
|
|
|
|
|
information does not directly influence the evaluation of the I |
196
|
|
|
|
|
|
|
expresssions. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
On Perls prior to 5.13.8, due to limitations of the API available to |
199
|
|
|
|
|
|
|
add-on parsing code, the form without parentheses is only available when |
200
|
|
|
|
|
|
|
it is the first thing in a statement. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=back |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 Catch blocks with dynamically-scoped names |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
This facility provides dynamic scoping of names for non-locally |
207
|
|
|
|
|
|
|
returnable blocks, while avoiding visible reification of continuations. |
208
|
|
|
|
|
|
|
The blocks can "catch" values that are "thrown" by lexically-remote code. |
209
|
|
|
|
|
|
|
(There is some resemblance here to throwing and catching of exceptions, |
210
|
|
|
|
|
|
|
but this is not an exception mechanism in itself.) |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
In each version, there is a code block which is labelled with a (possibly |
213
|
|
|
|
|
|
|
runtime-generated) string identifier I. In the C form, |
214
|
|
|
|
|
|
|
I must be stated as a double-quoted or single-quoted string syntax, |
215
|
|
|
|
|
|
|
possibly including variable interpolation. The tag is dynamically scoped, |
216
|
|
|
|
|
|
|
being visible during the execution of the block. The C function |
217
|
|
|
|
|
|
|
can then be used to return from (throw a value to) the dynamically |
218
|
|
|
|
|
|
|
enclosing block (the catch block) with a specified tag. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
The Common Lisp C and C operators allow any object to |
221
|
|
|
|
|
|
|
be used as a catch tag, and the tags are compared for object identity. |
222
|
|
|
|
|
|
|
This allows code to generate a catch tag that is guaranteed to be unique, |
223
|
|
|
|
|
|
|
simply by using a newly-allocated cons cell or similar object that is |
224
|
|
|
|
|
|
|
not referenced from anywhere else. If that sort of semantic is required, |
225
|
|
|
|
|
|
|
it is best implemented by using the C operator and |
226
|
|
|
|
|
|
|
saving the continuation reference in a Cised global variable. |
227
|
|
|
|
|
|
|
It is more usual for Common Lisp catch tags to be symbols, which |
228
|
|
|
|
|
|
|
idiomatically correspond to Perl strings, compared for string equality. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=over |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item catch TAG; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This form is a complete statement, ending with semicolon. |
235
|
|
|
|
|
|
|
The enclosing block (from this statement to the end of the block) is a |
236
|
|
|
|
|
|
|
catch block, tagged with I. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item catch TAG BLOCK |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This form is a complete statement, ending with the closing brace of |
241
|
|
|
|
|
|
|
I. I is executed normally. |
242
|
|
|
|
|
|
|
I is a catch block, tagged with I. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
It is unspecified whether I is evaluated inside or outside |
245
|
|
|
|
|
|
|
the block context. Do not rely on this aspect of its behaviour. |
246
|
|
|
|
|
|
|
(Historically it was inside, but outside makes more sense, so this may |
247
|
|
|
|
|
|
|
change in the future.) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item catch(TAG BLOCK) |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
This form is an expression. I is executed normally, and its |
252
|
|
|
|
|
|
|
return value will become the value of this expression. |
253
|
|
|
|
|
|
|
I is a catch block, tagged with I. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
It is unspecified whether I is evaluated inside or outside |
256
|
|
|
|
|
|
|
the block context. Do not rely on this aspect of its behaviour. |
257
|
|
|
|
|
|
|
(Historically it was inside, but outside makes more sense, so this may |
258
|
|
|
|
|
|
|
change in the future.) |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=item throw(TAG, VALUE ...) |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This is a function; all arguments are evaluated normally. It transfers |
263
|
|
|
|
|
|
|
control to exit from the dynamically enclosing catch block tagged |
264
|
|
|
|
|
|
|
with I. If there is no matching block, it is a runtime error. |
265
|
|
|
|
|
|
|
(Currently signalled by C, but this may change in the future.) |
266
|
|
|
|
|
|
|
Zero or more Is may be supplied, which determine what will be |
267
|
|
|
|
|
|
|
returned from the catch block. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
The Is are interpreted according to the syntactic context in |
270
|
|
|
|
|
|
|
which the target block was invoked. In void context, all the Is |
271
|
|
|
|
|
|
|
are ignored. In scalar context, only the last I is returned, |
272
|
|
|
|
|
|
|
or C if no Is were supplied. In list context, the full |
273
|
|
|
|
|
|
|
list of Is is used unmodified. Note that this non-local context |
274
|
|
|
|
|
|
|
information does not directly influence the evaluation of the I |
275
|
|
|
|
|
|
|
expresssions. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=back |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 BUGS |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
The constructs that declare lexically-scoped variables do not generate |
282
|
|
|
|
|
|
|
the "masks earlier declaration" warnings that they should. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
The lexical variable defined by C and |
285
|
|
|
|
|
|
|
C is writable. It really ought to be read-only. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
The custom parsing code required for most of the operators is only invoked |
288
|
|
|
|
|
|
|
if the operator is invoked using an unqualified name. For example, |
289
|
|
|
|
|
|
|
referring to C as C won't work. |
290
|
|
|
|
|
|
|
This limitation should be resolved if L or something |
291
|
|
|
|
|
|
|
similar migrates into the core in a future version of Perl. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
On Perls prior to 5.13.8, due to limitations of the API available to |
294
|
|
|
|
|
|
|
add-on parsing code, some of the operators are implemented by rewriting |
295
|
|
|
|
|
|
|
the source for the normal Perl parser to parse. This process risks |
296
|
|
|
|
|
|
|
unwanted interaction with other syntax-mutating modules, and is likely |
297
|
|
|
|
|
|
|
to break if the operators are imported under a non-standard name. |
298
|
|
|
|
|
|
|
The resulting failures are likely to be rather mystifying. |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
On Perls prior to 5.13.8, due to the aforementioned limitations of the |
301
|
|
|
|
|
|
|
API available to add-on parsing code, the version of C |
302
|
|
|
|
|
|
|
without parentheses is only available when it is the first thing in |
303
|
|
|
|
|
|
|
a statement. Since a C expression never returns locally, |
304
|
|
|
|
|
|
|
there is little reason for it to be a subexpression anyway. |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head1 SEE ALSO |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
L |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head1 AUTHOR |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
Andrew Main (Zefram) |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head1 COPYRIGHT |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
Copyright (C) 2010, 2011 Andrew Main (Zefram) |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 LICENSE |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
321
|
|
|
|
|
|
|
under the same terms as Perl itself. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=cut |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
1; |