1 package Devel::Declare;
2 # ABSTRACT: (DEPRECATED) Adding keywords to perl, in perl
8 our $VERSION = '0.006_020';
11 use constant DECLARE_NAME => 1;
12 use constant DECLARE_PROTO => 2;
13 use constant DECLARE_NONE => 4;
14 use constant DECLARE_PACKAGE => 8+1; # name implicit
16 use vars qw(%declarators %declarator_handlers @ISA);
17 use base qw(DynaLoader);
18 use Scalar::Util 'set_prototype';
19 use B::Hooks::OP::Check 0.19;
21 bootstrap Devel::Declare;
28 my ($class, %args) = @_;
30 if (@_ == 1) { # "use Devel::Declare;"
32 foreach my $name (qw(NAME PROTO NONE PACKAGE)) {
33 *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"};
36 $class->setup_for($target => \%args);
43 $class->teardown_for($target);
47 my ($class, $target, $args) = @_;
49 foreach my $key (keys %$args) {
50 my $info = $args->{$key};
52 if (ref($info) eq 'ARRAY') {
53 ($flags, $sub) = @$info;
54 } elsif (ref($info) eq 'CODE') {
55 $flags = DECLARE_NAME;
57 } elsif (ref($info) eq 'HASH') {
61 die "Info for sub ${key} must be [ \$flags, \$sub ] or \$sub or handler hashref";
63 $declarators{$target}{$key} = $flags;
64 $declarator_handlers{$target}{$key} = $sub;
69 my ($class, $target) = @_;
70 delete $declarators{$target};
71 delete $declarator_handlers{$target};
78 my ($usepack, $use, $inpack, $name, $proto, $traits) = @_;
79 my ($name_h, $XX_h, $extra_code)
80 = $declarator_handlers{$usepack}{$use}->(
81 $usepack, $use, $inpack, $name, $proto, defined(wantarray), $traits
83 ($temp_name, $temp_save) = ([], []);
85 $name = "${inpack}::${name}" unless $name =~ /::/;
86 shadow_sub($name, $name_h);
89 shadow_sub("${inpack}::X", $XX_h);
91 if (defined wantarray) {
92 return $extra_code || '0;';
100 push(@$temp_name, $name);
102 my ($pack, $pname) = ($name =~ m/(.+)::([^:]+)/);
103 push(@$temp_save, $pack->can($pname));
104 no warnings 'redefine';
105 no warnings 'prototype';
107 set_in_declare(~~@{$temp_name||[]});
112 my $name = shift(@{$temp_name||[]});
113 die "done_declare called with no temp_name stack" unless defined($name);
114 my $saved = shift(@$temp_save);
117 delete ${"${temp_pack}::"}{$name};
119 no warnings 'prototype';
120 *{"${temp_pack}::${name}"} = $saved;
122 set_in_declare(~~@{$temp_name||[]});
125 sub build_sub_installer {
126 my ($class, $pack, $name, $proto) = @_;
130 sub ${name} (${proto}) :lvalue {\n"
134 my $ret = $body->(@_);
137 sub { ($body) = @_; };';
140 sub setup_declarators {
141 my ($class, $pack, $to_setup) = @_;
142 die "${class}->setup_declarators(\$pack, \\\%to_setup)"
143 unless defined($pack) && ref($to_setup) eq 'HASH';
145 foreach my $name (keys %$to_setup) {
146 my $info = $to_setup->{$name};
147 my $flags = $info->{flags} || DECLARE_NAME;
148 my $run = $info->{run};
149 my $compile = $info->{compile};
150 my $proto = $info->{proto} || '&';
151 my $sub_proto = $proto;
152 # make all args optional to enable lvalue for DECLARE_NONE
153 $sub_proto =~ s/;//; $sub_proto = ';'.$sub_proto;
154 #my $installer = $class->build_sub_installer($pack, $name, $proto);
155 my $installer = $class->build_sub_installer($pack, $name, '@');
156 $installer->(sub :lvalue {
157 #{ no warnings 'uninitialized'; warn 'INST: '.join(', ', @_)."\n"; }
159 if (ref $_[0] eq 'HASH') {
162 my @ret = $run->(undef, undef, @_);
165 my $r = $run->(undef, undef, @_);
173 $setup_for_args{$name} = [
176 my ($usepack, $use, $inpack, $name, $proto, $shift_hashref, $traits) = @_;
177 my $extra_code = $compile->($name, $proto, $traits);
178 my $main_handler = sub { shift if $shift_hashref;
179 ("DONE", $run->($name, $proto, @_));
182 if (defined $proto) {
183 $name_h = sub :lvalue { return my $sv; };
185 } elsif (defined $name && length $name) {
186 $name_h = $main_handler;
189 $extra_code = '}, sub {'.$extra_code;
190 return ($name_h, $XX, $extra_code);
194 $class->setup_for($pack, \%setup_for_args);
197 sub install_declarator {
198 my ($class, $target_pack, $target_name, $flags, $filter, $handler) = @_;
199 $class->setup_declarators($target_pack, {
208 sub linestr_callback_rv2cv {
209 my ($name, $offset) = @_;
210 $offset += toke_move_past_token($offset);
211 my $pack = get_curstash_name();
212 my $flags = $declarators{$pack}{$name};
213 my ($found_name, $found_proto);
214 if ($flags & DECLARE_NAME) {
215 $offset += toke_skipspace($offset);
216 my $linestr = get_linestr();
217 if (substr($linestr, $offset, 2) eq '::') {
218 substr($linestr, $offset, 2) = '';
219 set_linestr($linestr);
221 if (my $len = toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
222 $found_name = substr($linestr, $offset, $len);
226 if ($flags & DECLARE_PROTO) {
227 $offset += toke_skipspace($offset);
228 my $linestr = get_linestr();
229 if (substr($linestr, $offset, 1) eq '(') {
230 my $length = toke_scan_str($offset);
231 $found_proto = get_lex_stuff();
234 ($found_name ? ' ' : '=')
235 .'X'.(' ' x length($found_proto));
236 $linestr = get_linestr();
237 substr($linestr, $offset, $length) = $replace;
238 set_linestr($linestr);
242 my @args = ($pack, $name, $pack, $found_name, $found_proto);
243 $offset += toke_skipspace($offset);
244 my $linestr = get_linestr();
245 if (substr($linestr, $offset, 1) eq '{') {
246 my $ret = init_declare(@args);
248 if (defined $ret && length $ret) {
249 substr($linestr, $offset, 0) = $ret;
250 set_linestr($linestr);
255 #warn "linestr now ${linestr}";
258 sub linestr_callback_const {
259 my ($name, $offset) = @_;
260 my $pack = get_curstash_name();
261 my $flags = $declarators{$pack}{$name};
262 if ($flags & DECLARE_NAME) {
263 $offset += toke_move_past_token($offset);
264 $offset += toke_skipspace($offset);
265 if (toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
266 my $linestr = get_linestr();
267 substr($linestr, $offset, 0) = '::';
268 set_linestr($linestr);
273 sub linestr_callback {
276 my $pack = get_curstash_name();
277 my $handlers = $declarator_handlers{$pack}{$name};
278 if (ref $handlers eq 'CODE') {
279 my $meth = "linestr_callback_${type}";
280 __PACKAGE__->can($meth)->(@_);
281 } elsif (ref $handlers eq 'HASH') {
282 if ($handlers->{$type}) {
283 $handlers->{$type}->(@_);
286 die "PANIC: unknown thing in handlers for $pack $name: $handlers";
292 Devel::Declare - Adding keywords to perl, in perl
296 use Method::Signatures;
301 # Use some new and exciting syntax like:
302 method hello (Str :$who, Int :$age where { $_ > 0 }) {
303 $self->say("Hello ${who}, I am ${age} years old!");
308 L<Devel::Declare> can install subroutines called declarators which locally take
309 over Perl's parser, allowing the creation of new syntax.
311 This document describes how to create a simple declarator.
315 =for comment mst wrote this warning for MooseX::Declare, and ether adapted it for here:
317 B<Warning:> Devel::Declare is a giant bag of crack
318 originally implemented by mst with the goal of upsetting the perl core
319 developers so much by its very existence that they implemented proper
320 keyword handling in the core.
322 As of perl5 version 14, this goal has been achieved, and modules such
323 as L<Devel::CallParser>, L<Function::Parameters>, and L<Keyword::Simple> provide
324 mechanisms to mangle perl syntax that don't require hallucinogenic
325 drugs to interpret the error messages they produce.
327 If you are using something that uses Devel::Declare, please for the love
328 of kittens use something else:
334 Instead of L<TryCatch>, use L<Try::Tiny>
338 Instead of L<Method::Signatures>, use
339 L<real subroutine signatures|perlsub/Signatures> (requires perl 5.22) or L<Moops>
345 We'll demonstrate the usage of C<Devel::Declare> with a motivating example: a new
346 C<method> keyword, which acts like the builtin C<sub>, but automatically unpacks
347 C<$self> and the other arguments.
352 =head2 Creating a declarator with C<setup_for>
354 You will typically create
360 Devel::Declare->setup_for(
362 { method => { const => \&parser } }
365 *{$caller.'::method'} = sub (&) {};
368 Starting from the end of this import routine, you'll see that we're creating a
369 subroutine called C<method> in the caller's namespace. Yes, that's just a normal
370 subroutine, and it does nothing at all (yet!) Note the prototype C<(&)> which means
371 that the caller would call it like so:
374 my ($self, $arg1, $arg2) = @_;
378 However we want to be able to call it like this
380 method foo ($arg1, $arg2) {
384 That's why we call C<setup_for> above, to register the declarator 'method' with a custom
385 parser, as per the next section. It acts on an optype, usually C<'const'> as above.
386 (Other valid values are C<'check'> and C<'rv2cv'>).
388 For a simpler way to install new methods, see also L<Devel::Declare::MethodInstaller::Simple>
390 =head2 Writing a parser subroutine
392 This subroutine is called at I<compilation> time, and allows you to read the custom
393 syntaxes that we want (in a syntax that may or may not be valid core Perl 5) and
394 munge it so that the result will be parsed by the C<perl> compiler.
396 For this example, we're defining some globals for convenience:
398 our ($Declarator, $Offset);
400 Then we define a parser subroutine to handle our declarator. We'll look at this in
404 local ($Declarator, $Offset) = @_;
406 C<Devel::Declare> provides some very low level utility methods to parse character
407 strings. We'll define some useful higher level routines below for convenience,
408 and we can use these to parse the various elements in our new syntax.
410 Notice how our parser subroutine is invoked at compile time,
411 when the C<perl> parser is pointed just I<before> the declarator name.
413 skip_declarator; # step past 'method'
414 my $name = strip_name; # strip out the name 'foo', if present
415 my $proto = strip_proto; # strip out the prototype '($arg1, $arg2)', if present
417 Now we can prepare some code to 'inject' into the new subroutine. For example we
418 might want the method as above to have C<my ($self, $arg1, $arg2) = @_> injected at
419 the beginning of it. We also do some clever stuff with scopes that we'll look
422 my $inject = make_proto_unwrap($proto);
424 $inject = scope_injector_call().$inject;
426 inject_if_block($inject);
428 We've now managed to change C<method ($arg1, $arg2) { ... }> into C<method {
429 injected_code; ... }>. This will compile... but we've lost the name of the
432 In a cute (or horrifying, depending on your perspective) trick, we temporarily
433 change the definition of the subroutine C<method> itself, to specialise it with
434 the C<$name> we stripped, so that it assigns the code block to that name.
436 Even though the I<next> time C<method> is compiled, it will be
437 redefined again, C<perl> caches these definitions in its parse
438 tree, so we'll always get the right one!
440 Note that we also handle the case where there was no name, allowing
441 an anonymous method analogous to an anonymous subroutine.
444 $name = join('::', Devel::Declare::get_curstash_name(), $name)
445 unless ($name =~ /::/);
446 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
448 shadow(sub (&) { shift });
453 =head2 Parser utilities in detail
455 For simplicity, we're using global variables like C<$Offset> in these examples.
456 You may prefer to look at L<Devel::Declare::Context::Simple>, which
457 encapsulates the context much more cleanly.
459 =head3 C<skip_declarator>
461 This simple parser just moves across a 'token'. The common case is
462 to skip the declarator, i.e. to move to the end of the string
463 'method' and before the prototype and code block.
465 sub skip_declarator {
466 $Offset += Devel::Declare::toke_move_past_token($Offset);
469 =head4 C<toke_move_past_token>
471 This builtin parser simply moves past a 'token' (matching C</[a-zA-Z_]\w*/>)
472 It takes an offset into the source document, and skips past the token.
473 It returns the number of characters skipped.
477 This parser skips any whitespace, then scans the next word (again matching a
478 'token'). We can then analyse the current line, and manipulate it (using pure
479 Perl). In this case we take the name of the method out, and return it.
483 if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) {
484 my $linestr = Devel::Declare::get_linestr();
485 my $name = substr($linestr, $Offset, $len);
486 substr($linestr, $Offset, $len) = '';
487 Devel::Declare::set_linestr($linestr);
493 =head4 C<toke_scan_word>
495 This builtin parser, given an offset into the source document,
496 matches a 'token' as above but does not skip. It returns the
497 length of the token matched, if any.
499 =head4 C<get_linestr>
501 This builtin returns the full text of the current line of the source document.
503 =head4 C<set_linestr>
505 This builtin sets the full text of the current line of the source document.
506 Beware that injecting a newline into the middle of the line is likely
507 to fail in surprising ways. Generally, Perl's parser can rely on the
508 `current line' actually being only a single line. Use other kinds of
509 whitespace instead, in the code that you inject.
513 This parser skips whitsepace.
516 $Offset += Devel::Declare::toke_skipspace($Offset);
519 =head4 C<toke_skipspace>
521 This builtin parser, given an offset into the source document,
522 skips over any whitespace, and returns the number of characters
525 =head3 C<strip_proto>
527 This is a more complex parser that checks if it's found something that
528 starts with C<'('> and returns everything till the matching C<')'>.
533 my $linestr = Devel::Declare::get_linestr();
534 if (substr($linestr, $Offset, 1) eq '(') {
535 my $length = Devel::Declare::toke_scan_str($Offset);
536 my $proto = Devel::Declare::get_lex_stuff();
537 Devel::Declare::clear_lex_stuff();
538 $linestr = Devel::Declare::get_linestr();
539 substr($linestr, $Offset, $length) = '';
540 Devel::Declare::set_linestr($linestr);
546 =head4 C<toke_scan_str>
548 This builtin parser uses Perl's own parsing routines to match a "stringlike"
549 expression. Handily, this includes bracketed expressions (just think about
550 things like C<q(this is a quote)>).
552 Also it Does The Right Thing with nested delimiters (like C<q(this (is (a) quote))>).
554 It returns the effective length of the expression matched. Really, what
555 it returns is the difference in position between where the string started,
556 within the buffer, and where it finished. If the string extended across
557 multiple lines then the contents of the buffer may have been completely
558 replaced by the new lines, so this position difference is not the same
559 thing as the actual length of the expression matched. However, because
560 moving backward in the buffer causes problems, the function arranges
561 for the effective length to always be positive, padding the start of
562 the buffer if necessary.
564 Use C<get_lex_stuff> to get the actual matched text, the content of
565 the string. Because of the behaviour around multiline strings, you
566 can't reliably get this from the buffer. In fact, after the function
567 returns, you can't rely on any content of the buffer preceding the end
570 If the string being scanned is not well formed (has no closing delimiter),
571 C<toke_scan_str> returns C<undef>. In this case you cannot rely on the
572 contents of the buffer.
574 =head4 C<get_lex_stuff>
576 This builtin returns what was matched by C<toke_scan_str>. To avoid segfaults,
577 you should call C<clear_lex_stuff> immediately afterwards.
579 =head2 Munging the subroutine
581 Let's look at what we need to do in detail.
583 =head3 C<make_proto_unwrap>
585 We may have defined our method in different ways, which will result
586 in a different value for our prototype, as parsed above. For example:
588 method foo { # undefined
590 method foo ($arg1) { # '$arg1'
592 We deal with them as follows, and return the appropriate C<my ($self, ...) = @_;>
595 sub make_proto_unwrap {
597 my $inject = 'my ($self';
598 if (defined $proto) {
599 $inject .= ", $proto" if length($proto);
600 $inject .= ') = @_; ';
602 $inject .= ') = shift;';
607 =head3 C<inject_if_block>
609 Now we need to inject it after the opening C<'{'> of the method body.
610 We can do this with the building blocks we defined above like C<skipspace>
613 sub inject_if_block {
616 my $linestr = Devel::Declare::get_linestr;
617 if (substr($linestr, $Offset, 1) eq '{') {
618 substr($linestr, $Offset+1, 0) = $inject;
619 Devel::Declare::set_linestr($linestr);
623 =head3 C<scope_injector_call>
625 We want to be able to handle both named and anonymous methods. i.e.
627 method foo () { ... }
628 my $meth = method () { ... };
630 These will then get rewritten as
633 my $meth = method { ... };
635 where 'method' is a subroutine that takes a code block. Spot the problem?
636 The first one doesn't have a semicolon at the end of it! Unlike 'sub' which
637 is a builtin, this is just a normal statement, so we need to terminate it.
638 Luckily, using C<B::Hooks::EndOfScope>, we can do this!
640 use B::Hooks::EndOfScope;
642 We'll add this to what gets 'injected' at the beginning of the method source.
644 sub scope_injector_call {
645 return ' BEGIN { MethodHandlers::inject_scope }; ';
648 So at the beginning of every method, we are passing a callback that will get invoked
649 at the I<end> of the method's compilation... i.e. exactly then the closing C<'}'>
654 my $linestr = Devel::Declare::get_linestr;
655 my $offset = Devel::Declare::get_linestr_offset;
656 substr($linestr, $offset, 0) = ';';
657 Devel::Declare::set_linestr($linestr);
661 =head2 Shadowing each method.
665 We override the current definition of 'method' using C<shadow>.
668 my $pack = Devel::Declare::get_curstash_name;
669 Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]);
672 For a named method we invoked like this:
674 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
676 So in the case of a C<method foo { ... }>, this call would redefine C<method>
677 to be a subroutine that exports 'sub foo' as the (munged) contents of C<{...}>.
679 The case of an anonymous method is also cute:
681 shadow(sub (&) { shift });
685 my $meth = method () { ... };
687 is rewritten with C<method> taking the codeblock, and returning it as is to become
688 the value of C<$meth>.
690 =head4 C<get_curstash_name>
692 This returns the package name I<currently being compiled>.
696 Handles the details of redefining the subroutine.
700 One of the best ways to learn C<Devel::Declare> is still to look at
703 L<http://cpants.perl.org/dist/used_by/Devel-Declare>.
707 Matt S Trout - E<lt>mst@shadowcat.co.ukE<gt> - original author
709 Company: http://www.shadowcat.co.uk/
710 Blog: http://chainsawblues.vox.com/
712 Florian Ragwitz E<lt>rafl@debian.orgE<gt> - maintainer
714 osfameron E<lt>osfameron@cpan.orgE<gt> - first draft of documentation
716 =head1 COPYRIGHT AND LICENSE
718 This library is free software under the same terms as perl itself
720 Copyright (c) 2007, 2008, 2009 Matt S Trout
722 Copyright (c) 2008, 2009 Florian Ragwitz
724 stolen_chunk_of_toke.c based on toke.c from the perl core, which is
726 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
727 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others