1NAME
2 Devel::Declare - (DEPRECATED) Adding keywords to perl, in perl
3
4SYNOPSIS
5 use Method::Signatures;
6 # or ...
7 use MooseX::Declare;
8 # etc.
9
10 # Use some new and exciting syntax like:
11 method hello (Str :$who, Int :$age where { $_ > 0 }) {
12 $self->say("Hello ${who}, I am ${age} years old!");
13 }
14
15DESCRIPTION
16 Devel::Declare can install subroutines called declarators which locally
17 take over Perl's parser, allowing the creation of new syntax.
18
19 This document describes how to create a simple declarator.
20
21WARNING
22 Warning: Devel::Declare is a giant bag of crack originally implemented
23 by mst with the goal of upsetting the perl core developers so much by
24 its very existence that they implemented proper keyword handling in the
25 core.
26
27 As of perl5 version 14, this goal has been achieved, and modules such as
28 Devel::CallParser, Function::Parameters, and Keyword::Simple provide
29 mechanisms to mangle perl syntax that don't require hallucinogenic drugs
30 to interpret the error messages they produce.
31
32 If you are using something that uses Devel::Declare, please for the love
33 of kittens use something else:
34
35 * Instead of TryCatch, use Try::Tiny
36
37 * Instead of Method::Signatures, use real subroutine signatures
38 (requires perl 5.22) or Moops
39
40USAGE
41 We'll demonstrate the usage of "Devel::Declare" with a motivating
42 example: a new "method" keyword, which acts like the builtin "sub", but
43 automatically unpacks $self and the other arguments.
44
45 package My::Methods;
46 use Devel::Declare;
47
48 Creating a declarator with "setup_for"
49 You will typically create
50
51 sub import {
52 my $class = shift;
53 my $caller = caller;
54
55 Devel::Declare->setup_for(
56 $caller,
57 { method => { const => \&parser } }
58 );
59 no strict 'refs';
60 *{$caller.'::method'} = sub (&) {};
61 }
62
63 Starting from the end of this import routine, you'll see that we're
64 creating a subroutine called "method" in the caller's namespace. Yes,
65 that's just a normal subroutine, and it does nothing at all (yet!) Note
66 the prototype "(&)" which means that the caller would call it like so:
67
68 method {
69 my ($self, $arg1, $arg2) = @_;
70 ...
71 }
72
73 However we want to be able to call it like this
74
75 method foo ($arg1, $arg2) {
76 ...
77 }
78
79 That's why we call "setup_for" above, to register the declarator
80 'method' with a custom parser, as per the next section. It acts on an
81 optype, usually 'const' as above. (Other valid values are 'check' and
82 'rv2cv').
83
84 For a simpler way to install new methods, see also
85 Devel::Declare::MethodInstaller::Simple
86
87 Writing a parser subroutine
88 This subroutine is called at *compilation* time, and allows you to read
89 the custom syntaxes that we want (in a syntax that may or may not be
90 valid core Perl 5) and munge it so that the result will be parsed by the
91 "perl" compiler.
92
93 For this example, we're defining some globals for convenience:
94
95 our ($Declarator, $Offset);
96
97 Then we define a parser subroutine to handle our declarator. We'll look
98 at this in a few chunks.
99
100 sub parser {
101 local ($Declarator, $Offset) = @_;
102
103 "Devel::Declare" provides some very low level utility methods to parse
104 character strings. We'll define some useful higher level routines below
105 for convenience, and we can use these to parse the various elements in
106 our new syntax.
107
108 Notice how our parser subroutine is invoked at compile time, when the
109 "perl" parser is pointed just *before* the declarator name.
110
111 skip_declarator; # step past 'method'
112 my $name = strip_name; # strip out the name 'foo', if present
113 my $proto = strip_proto; # strip out the prototype '($arg1, $arg2)', if present
114
115 Now we can prepare some code to 'inject' into the new subroutine. For
116 example we might want the method as above to have "my ($self, $arg1,
117 $arg2) = @_" injected at the beginning of it. We also do some clever
118 stuff with scopes that we'll look at shortly.
119
120 my $inject = make_proto_unwrap($proto);
121 if (defined $name) {
122 $inject = scope_injector_call().$inject;
123 }
124 inject_if_block($inject);
125
126 We've now managed to change "method ($arg1, $arg2) { ... }" into "method
127 { injected_code; ... }". This will compile... but we've lost the name of
128 the method!
129
130 In a cute (or horrifying, depending on your perspective) trick, we
131 temporarily change the definition of the subroutine "method" itself, to
132 specialise it with the $name we stripped, so that it assigns the code
133 block to that name.
134
135 Even though the *next* time "method" is compiled, it will be redefined
136 again, "perl" caches these definitions in its parse tree, so we'll
137 always get the right one!
138
139 Note that we also handle the case where there was no name, allowing an
140 anonymous method analogous to an anonymous subroutine.
141
142 if (defined $name) {
143 $name = join('::', Devel::Declare::get_curstash_name(), $name)
144 unless ($name =~ /::/);
145 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
146 } else {
147 shadow(sub (&) { shift });
148 }
149 }
150
151 Parser utilities in detail
152 For simplicity, we're using global variables like $Offset in these
153 examples. You may prefer to look at Devel::Declare::Context::Simple,
154 which encapsulates the context much more cleanly.
155
156 "skip_declarator"
157 This simple parser just moves across a 'token'. The common case is to
158 skip the declarator, i.e. to move to the end of the string 'method' and
159 before the prototype and code block.
160
161 sub skip_declarator {
162 $Offset += Devel::Declare::toke_move_past_token($Offset);
163 }
164
165 "toke_move_past_token"
166 This builtin parser simply moves past a 'token' (matching
167 "/[a-zA-Z_]\w*/") It takes an offset into the source document, and skips
168 past the token. It returns the number of characters skipped.
169
170 "strip_name"
171 This parser skips any whitespace, then scans the next word (again
172 matching a 'token'). We can then analyse the current line, and
173 manipulate it (using pure Perl). In this case we take the name of the
174 method out, and return it.
175
176 sub strip_name {
177 skipspace;
178 if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) {
179 my $linestr = Devel::Declare::get_linestr();
180 my $name = substr($linestr, $Offset, $len);
181 substr($linestr, $Offset, $len) = '';
182 Devel::Declare::set_linestr($linestr);
183 return $name;
184 }
185 return;
186 }
187
188 "toke_scan_word"
189 This builtin parser, given an offset into the source document, matches a
190 'token' as above but does not skip. It returns the length of the token
191 matched, if any.
192
193 "get_linestr"
194 This builtin returns the full text of the current line of the source
195 document.
196
197 "set_linestr"
198 This builtin sets the full text of the current line of the source
199 document. Beware that injecting a newline into the middle of the line is
200 likely to fail in surprising ways. Generally, Perl's parser can rely on
201 the `current line' actually being only a single line. Use other kinds of
202 whitespace instead, in the code that you inject.
203
204 "skipspace"
205 This parser skips whitsepace.
206
207 sub skipspace {
208 $Offset += Devel::Declare::toke_skipspace($Offset);
209 }
210
211 "toke_skipspace"
212 This builtin parser, given an offset into the source document, skips
213 over any whitespace, and returns the number of characters skipped.
214
215 "strip_proto"
216 This is a more complex parser that checks if it's found something that
217 starts with '(' and returns everything till the matching ')'.
218
219 sub strip_proto {
220 skipspace;
221
222 my $linestr = Devel::Declare::get_linestr();
223 if (substr($linestr, $Offset, 1) eq '(') {
224 my $length = Devel::Declare::toke_scan_str($Offset);
225 my $proto = Devel::Declare::get_lex_stuff();
226 Devel::Declare::clear_lex_stuff();
227 $linestr = Devel::Declare::get_linestr();
228 substr($linestr, $Offset, $length) = '';
229 Devel::Declare::set_linestr($linestr);
230 return $proto;
231 }
232 return;
233 }
234
235 "toke_scan_str"
236 This builtin parser uses Perl's own parsing routines to match a
237 "stringlike" expression. Handily, this includes bracketed expressions
238 (just think about things like "q(this is a quote)").
239
240 Also it Does The Right Thing with nested delimiters (like "q(this (is
241 (a) quote))").
242
243 It returns the effective length of the expression matched. Really, what
244 it returns is the difference in position between where the string
245 started, within the buffer, and where it finished. If the string
246 extended across multiple lines then the contents of the buffer may have
247 been completely replaced by the new lines, so this position difference
248 is not the same thing as the actual length of the expression matched.
249 However, because moving backward in the buffer causes problems, the
250 function arranges for the effective length to always be positive,
251 padding the start of the buffer if necessary.
252
253 Use "get_lex_stuff" to get the actual matched text, the content of the
254 string. Because of the behaviour around multiline strings, you can't
255 reliably get this from the buffer. In fact, after the function returns,
256 you can't rely on any content of the buffer preceding the end of the
257 string.
258
259 If the string being scanned is not well formed (has no closing
260 delimiter), "toke_scan_str" returns "undef". In this case you cannot
261 rely on the contents of the buffer.
262
263 "get_lex_stuff"
264 This builtin returns what was matched by "toke_scan_str". To avoid
265 segfaults, you should call "clear_lex_stuff" immediately afterwards.
266
267 Munging the subroutine
268 Let's look at what we need to do in detail.
269
270 "make_proto_unwrap"
271 We may have defined our method in different ways, which will result in a
272 different value for our prototype, as parsed above. For example:
273
274 method foo { # undefined
275 method foo () { # ''
276 method foo ($arg1) { # '$arg1'
277
278 We deal with them as follows, and return the appropriate "my ($self,
279 ...) = @_;" string.
280
281 sub make_proto_unwrap {
282 my ($proto) = @_;
283 my $inject = 'my ($self';
284 if (defined $proto) {
285 $inject .= ", $proto" if length($proto);
286 $inject .= ') = @_; ';
287 } else {
288 $inject .= ') = shift;';
289 }
290 return $inject;
291 }
292
293 "inject_if_block"
294 Now we need to inject it after the opening '{' of the method body. We
295 can do this with the building blocks we defined above like "skipspace"
296 and "get_linestr".
297
298 sub inject_if_block {
299 my $inject = shift;
300 skipspace;
301 my $linestr = Devel::Declare::get_linestr;
302 if (substr($linestr, $Offset, 1) eq '{') {
303 substr($linestr, $Offset+1, 0) = $inject;
304 Devel::Declare::set_linestr($linestr);
305 }
306 }
307
308 "scope_injector_call"
309 We want to be able to handle both named and anonymous methods. i.e.
310
311 method foo () { ... }
312 my $meth = method () { ... };
313
314 These will then get rewritten as
315
316 method { ... }
317 my $meth = method { ... };
318
319 where 'method' is a subroutine that takes a code block. Spot the
320 problem? The first one doesn't have a semicolon at the end of it! Unlike
321 'sub' which is a builtin, this is just a normal statement, so we need to
322 terminate it. Luckily, using "B::Hooks::EndOfScope", we can do this!
323
324 use B::Hooks::EndOfScope;
325
326 We'll add this to what gets 'injected' at the beginning of the method
327 source.
328
329 sub scope_injector_call {
330 return ' BEGIN { MethodHandlers::inject_scope }; ';
331 }
332
333 So at the beginning of every method, we are passing a callback that will
334 get invoked at the *end* of the method's compilation... i.e. exactly
335 then the closing '}' is compiled.
336
337 sub inject_scope {
338 on_scope_end {
339 my $linestr = Devel::Declare::get_linestr;
340 my $offset = Devel::Declare::get_linestr_offset;
341 substr($linestr, $offset, 0) = ';';
342 Devel::Declare::set_linestr($linestr);
343 };
344 }
345
346 Shadowing each method.
347 "shadow"
348 We override the current definition of 'method' using "shadow".
349
350 sub shadow {
351 my $pack = Devel::Declare::get_curstash_name;
352 Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]);
353 }
354
355 For a named method we invoked like this:
356
357 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
358
359 So in the case of a "method foo { ... }", this call would redefine
360 "method" to be a subroutine that exports 'sub foo' as the (munged)
361 contents of "{...}".
362
363 The case of an anonymous method is also cute:
364
365 shadow(sub (&) { shift });
366
367 This means that
368
369 my $meth = method () { ... };
370
371 is rewritten with "method" taking the codeblock, and returning it as is
372 to become the value of $meth.
373
374 "get_curstash_name"
375 This returns the package name *currently being compiled*.
376
377 "shadow_sub"
378 Handles the details of redefining the subroutine.
379
380SEE ALSO
381 One of the best ways to learn "Devel::Declare" is still to look at
382 modules that use it:
383
384 <http://cpants.perl.org/dist/used_by/Devel-Declare>.
385
386AUTHORS
387 Matt S Trout - <mst@shadowcat.co.uk> - original author
388
389 Company: http://www.shadowcat.co.uk/ Blog: http://chainsawblues.vox.com/
390
391 Florian Ragwitz <rafl@debian.org> - maintainer
392
393 osfameron <osfameron@cpan.org> - first draft of documentation
394
395COPYRIGHT AND LICENSE
396 This library is free software under the same terms as perl itself
397
398 Copyright (c) 2007, 2008, 2009 Matt S Trout
399
400 Copyright (c) 2008, 2009 Florian Ragwitz
401
402 stolen_chunk_of_toke.c based on toke.c from the perl core, which is
403
404 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
405 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
406
407