1 package Devel::Declare;
7 our $VERSION = '0.005011';
9 use constant DECLARE_NAME => 1;
10 use constant DECLARE_PROTO => 2;
11 use constant DECLARE_NONE => 4;
12 use constant DECLARE_PACKAGE => 8+1; # name implicit
14 use vars qw(%declarators %declarator_handlers @ISA);
15 use base qw(DynaLoader);
16 use Scalar::Util 'set_prototype';
17 use B::Hooks::OP::Check;
19 bootstrap Devel::Declare;
24 my ($class, %args) = @_;
26 if (@_ == 1) { # "use Devel::Declare;"
28 foreach my $name (qw(NAME PROTO NONE PACKAGE)) {
29 *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"};
32 $class->setup_for($target => \%args);
39 $class->teardown_for($target);
43 my ($class, $target, $args) = @_;
45 foreach my $key (keys %$args) {
46 my $info = $args->{$key};
48 if (ref($info) eq 'ARRAY') {
49 ($flags, $sub) = @$info;
50 } elsif (ref($info) eq 'CODE') {
51 $flags = DECLARE_NAME;
53 } elsif (ref($info) eq 'HASH') {
57 die "Info for sub ${key} must be [ \$flags, \$sub ] or \$sub or handler hashref";
59 $declarators{$target}{$key} = $flags;
60 $declarator_handlers{$target}{$key} = $sub;
65 my ($class, $target) = @_;
66 delete $declarators{$target};
67 delete $declarator_handlers{$target};
74 my ($usepack, $use, $inpack, $name, $proto, $traits) = @_;
75 my ($name_h, $XX_h, $extra_code)
76 = $declarator_handlers{$usepack}{$use}->(
77 $usepack, $use, $inpack, $name, $proto, defined(wantarray), $traits
79 ($temp_name, $temp_save) = ([], []);
81 $name = "${inpack}::${name}" unless $name =~ /::/;
82 shadow_sub($name, $name_h);
85 shadow_sub("${inpack}::X", $XX_h);
87 if (defined wantarray) {
88 return $extra_code || '0;';
96 push(@$temp_name, $name);
98 my ($pack, $pname) = ($name =~ m/(.+)::([^:]+)/);
99 push(@$temp_save, $pack->can($pname));
100 delete ${ "${pack}::" }{$pname};
101 no warnings 'redefine';
102 no warnings 'prototype';
104 set_in_declare(~~@{$temp_name||[]});
109 my $name = shift(@{$temp_name||[]});
110 die "done_declare called with no temp_name stack" unless defined($name);
111 my $saved = shift(@$temp_save);
114 delete ${"${temp_pack}::"}{$name};
116 no warnings 'prototype';
117 *{"${temp_pack}::${name}"} = $saved;
119 set_in_declare(~~@{$temp_name||[]});
122 sub build_sub_installer {
123 my ($class, $pack, $name, $proto) = @_;
127 sub ${name} (${proto}) :lvalue {\n"
131 my $ret = $body->(@_);
134 sub { ($body) = @_; };';
137 sub setup_declarators {
138 my ($class, $pack, $to_setup) = @_;
139 die "${class}->setup_declarators(\$pack, \\\%to_setup)"
140 unless defined($pack) && ref($to_setup) eq 'HASH';
142 foreach my $name (keys %$to_setup) {
143 my $info = $to_setup->{$name};
144 my $flags = $info->{flags} || DECLARE_NAME;
145 my $run = $info->{run};
146 my $compile = $info->{compile};
147 my $proto = $info->{proto} || '&';
148 my $sub_proto = $proto;
149 # make all args optional to enable lvalue for DECLARE_NONE
150 $sub_proto =~ s/;//; $sub_proto = ';'.$sub_proto;
151 #my $installer = $class->build_sub_installer($pack, $name, $proto);
152 my $installer = $class->build_sub_installer($pack, $name, '@');
153 $installer->(sub :lvalue {
154 #{ no warnings 'uninitialized'; warn 'INST: '.join(', ', @_)."\n"; }
156 if (ref $_[0] eq 'HASH') {
159 my @ret = $run->(undef, undef, @_);
162 my $r = $run->(undef, undef, @_);
170 $setup_for_args{$name} = [
173 my ($usepack, $use, $inpack, $name, $proto, $shift_hashref, $traits) = @_;
174 my $extra_code = $compile->($name, $proto, $traits);
175 my $main_handler = sub { shift if $shift_hashref;
176 ("DONE", $run->($name, $proto, @_));
179 if (defined $proto) {
180 $name_h = sub :lvalue { return my $sv; };
182 } elsif (defined $name && length $name) {
183 $name_h = $main_handler;
186 $extra_code = '}, sub {'.$extra_code;
187 return ($name_h, $XX, $extra_code);
191 $class->setup_for($pack, \%setup_for_args);
194 sub install_declarator {
195 my ($class, $target_pack, $target_name, $flags, $filter, $handler) = @_;
196 $class->setup_declarators($target_pack, {
205 sub linestr_callback_rv2cv {
206 my ($name, $offset) = @_;
207 $offset += toke_move_past_token($offset);
208 my $pack = get_curstash_name();
209 my $flags = $declarators{$pack}{$name};
210 my ($found_name, $found_proto);
211 if ($flags & DECLARE_NAME) {
212 $offset += toke_skipspace($offset);
213 my $linestr = get_linestr();
214 if (substr($linestr, $offset, 2) eq '::') {
215 substr($linestr, $offset, 2) = '';
216 set_linestr($linestr);
218 if (my $len = toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
219 $found_name = substr($linestr, $offset, $len);
223 if ($flags & DECLARE_PROTO) {
224 $offset += toke_skipspace($offset);
225 my $linestr = get_linestr();
226 if (substr($linestr, $offset, 1) eq '(') {
227 my $length = toke_scan_str($offset);
228 $found_proto = get_lex_stuff();
231 ($found_name ? ' ' : '=')
232 .'X'.(' ' x length($found_proto));
233 $linestr = get_linestr();
234 substr($linestr, $offset, $length) = $replace;
235 set_linestr($linestr);
239 my @args = ($pack, $name, $pack, $found_name, $found_proto);
240 $offset += toke_skipspace($offset);
241 my $linestr = get_linestr();
242 if (substr($linestr, $offset, 1) eq '{') {
243 my $ret = init_declare(@args);
245 if (defined $ret && length $ret) {
246 substr($linestr, $offset, 0) = $ret;
247 set_linestr($linestr);
252 #warn "linestr now ${linestr}";
255 sub linestr_callback_const {
256 my ($name, $offset) = @_;
257 my $pack = get_curstash_name();
258 my $flags = $declarators{$pack}{$name};
259 if ($flags & DECLARE_NAME) {
260 $offset += toke_move_past_token($offset);
261 $offset += toke_skipspace($offset);
262 if (toke_scan_word($offset, $flags & DECLARE_PACKAGE)) {
263 my $linestr = get_linestr();
264 substr($linestr, $offset, 0) = '::';
265 set_linestr($linestr);
270 sub linestr_callback {
273 my $pack = get_curstash_name();
274 my $handlers = $declarator_handlers{$pack}{$name};
275 if (ref $handlers eq 'CODE') {
276 my $meth = "linestr_callback_${type}";
277 __PACKAGE__->can($meth)->(@_);
278 } elsif (ref $handlers eq 'HASH') {
279 if ($handlers->{$type}) {
280 $handlers->{$type}->(@_);
283 die "PANIC: unknown thing in handlers for $pack $name: $handlers";
289 Devel::Declare - Adding keywords to perl, in perl
293 use Method::Signatures;
298 # Use some new and exciting syntax like:
299 method hello (Str :$who, Int :$age where { $_ > 0 }) {
300 $self->say("Hello ${who}, I am ${age} years old!");
305 L<Devel::Declare> can install subroutines called declarators which locally take
306 over Perl's parser, allowing the creation of new syntax.
308 This document describes how to create a simple declarator.
312 We'll demonstrate the usage of C<Devel::Declare> with a motivating example: a new
313 C<method> keyword, which acts like the builtin C<sub>, but automatically unpacks
314 C<$self> and the other arguments.
319 =head2 Creating a declarator with C<setup_for>
321 You will typically create
327 Devel::Declare->setup_for(
329 { method => { const => \&parser } }
332 *{$caller.'::method'} = sub (&) {};
335 Starting from the end of this import routine, you'll see that we're creating a
336 subroutine called C<method> in the caller's namespace. Yes, that's just a normal
337 subroutine, and it does nothing at all (yet!) Note the prototype C<(&)> which means
338 that the caller would call it like so:
341 my ($self, $arg1, $arg2) = @_;
345 However we want to be able to call it like this
347 method foo ($arg1, $arg2) {
351 That's why we call C<setup_for> above, to register the declarator 'method' with a custom
352 parser, as per the next section. It acts on an optype, usually C<'const'> as above.
353 (Other valid values are C<'check'> and C<'rv2cv'>).
355 For a simpler way to install new methods, see also L<Devel::Declare::MethodInstaller::Simple>
357 =head2 Writing a parser subroutine
359 This subroutine is called at I<compilation> time, and allows you to read the custom
360 syntaxes that we want (in a syntax that may or may not be valid core Perl 5) and
361 munge it so that the result will be parsed by the C<perl> compiler.
363 For this example, we're defining some globals for convenience:
365 our ($Declarator, $Offset);
367 Then we define a parser subroutine to handle our declarator. We'll look at this in
371 local ($Declarator, $Offset) = @_;
373 C<Devel::Declare> provides some very low level utility methods to parse character
374 strings. We'll define some useful higher level routines below for convenience,
375 and we can use these to parse the various elements in our new syntax.
377 Notice how our parser subroutine is invoked at compile time,
378 when the C<perl> parser is pointed just I<before> the declarator name.
380 skip_declarator; # step past 'method'
381 my $name = strip_name; # strip out the name 'foo', if present
382 my $proto = strip_proto; # strip out the prototype '($arg1, $arg2)', if present
384 Now we can prepare some code to 'inject' into the new subroutine. For example we
385 might want the method as above to have C<my ($self, $arg1, $arg2) = @_> injected at
386 the beginning of it. We also do some clever stuff with scopes that we'll look
389 my $inject = make_proto_unwrap($proto);
391 $inject = scope_injector_call().$inject;
393 inject_if_block($inject);
395 We've now managed to change C<method ($arg1, $arg2) { ... }> into C<method {
396 injected_code; ... }>. This will compile... but we've lost the name of the
399 In a cute (or horrifying, depending on your perspective) trick, we temporarily
400 change the definition of the subroutine C<method> itself, to specialise it with
401 the C<$name> we stripped, so that it assigns the code block to that name.
403 Even though the I<next> time C<method> is compiled, it will be
404 redefined again, C<perl> caches these definitions in its parse
405 tree, so we'll always get the right one!
407 Note that we also handle the case where there was no name, allowing
408 an anonymous method analogous to an anonymous subroutine.
411 $name = join('::', Devel::Declare::get_curstash_name(), $name)
412 unless ($name =~ /::/);
413 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
415 shadow(sub (&) { shift });
420 =head2 Parser utilities in detail
422 For simplicity, we're using global variables like C<$Offset> in these examples.
423 You may prefer to look at L<Devel::Declare::Context::Simple>, which
424 encapsulates the context much more cleanly.
426 =head3 C<skip_declarator>
428 This simple parser just moves across a 'token'. The common case is
429 to skip the declarator, i.e. to move to the end of the string
430 'method' and before the prototype and code block.
432 sub skip_declarator {
433 $Offset += Devel::Declare::toke_move_past_token($Offset);
436 =head4 C<toke_move_past_token>
438 This builtin parser simply moves past a 'token' (matching C</[a-zA-Z_]\w*/>)
439 It takes an offset into the source document, and skips past the token.
440 It returns the number of characters skipped.
444 This parser skips any whitespace, then scans the next word (again matching a
445 'token'). We can then analyse the current line, and manipulate it (using pure
446 Perl). In this case we take the name of the method out, and return it.
450 if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) {
451 my $linestr = Devel::Declare::get_linestr();
452 my $name = substr($linestr, $Offset, $len);
453 substr($linestr, $Offset, $len) = '';
454 Devel::Declare::set_linestr($linestr);
460 =head4 C<toke_scan_word>
462 This builtin parser, given an offset into the source document,
463 matches a 'token' as above but does not skip. It returns the
464 length of the token matched, if any.
466 =head4 C<get_linestr>
468 This builtin returns the full text of the current line of the source document.
470 =head4 C<set_linestr>
472 This builtin sets the full text of the current line of the source document.
476 This parser skips whitsepace.
479 $Offset += Devel::Declare::toke_skipspace($Offset);
482 =head4 C<toke_skipspace>
484 This builtin parser, given an offset into the source document,
485 skips over any whitespace, and returns the number of characters
488 =head3 C<strip_proto>
490 This is a more complex parser that checks if it's found something that
491 starts with C<'('> and returns everything till the matching C<')'>.
496 my $linestr = Devel::Declare::get_linestr();
497 if (substr($linestr, $Offset, 1) eq '(') {
498 my $length = Devel::Declare::toke_scan_str($Offset);
499 my $proto = Devel::Declare::get_lex_stuff();
500 Devel::Declare::clear_lex_stuff();
501 $linestr = Devel::Declare::get_linestr();
502 substr($linestr, $Offset, $length) = '';
503 Devel::Declare::set_linestr($linestr);
509 =head4 C<toke_scan_str>
511 This builtin parser uses Perl's own parsing routines to match a "stringlike"
512 expression. Handily, this includes bracketed expressions (just think about
513 things like C<q(this is a quote)>).
515 Also it Does The Right Thing with nested delimiters (like C<q(this (is (a) quote))>).
517 It returns the length of the expression matched. Use C<get_lex_stuff> to
518 get the actual matched text.
520 =head4 C<get_lex_stuff>
522 This builtin returns what was matched by C<toke_scan_str>. To avoid segfaults,
523 you should call C<clear_lex_stuff> immediately afterwards.
525 =head2 Munging the subroutine
527 Let's look at what we need to do in detail.
529 =head3 C<make_proto_unwrap>
531 We may have defined our method in different ways, which will result
532 in a different value for our prototype, as parsed above. For example:
534 method foo { # undefined
536 method foo ($arg1) { # '$arg1'
538 We deal with them as follows, and return the appropriate C<my ($self, ...) = @_;>
541 sub make_proto_unwrap {
543 my $inject = 'my ($self';
544 if (defined $proto) {
545 $inject .= ", $proto" if length($proto);
546 $inject .= ') = @_; ';
548 $inject .= ') = shift;';
553 =head3 C<inject_if_block>
555 Now we need to inject it after the opening C<'{'> of the method body.
556 We can do this with the building blocks we defined above like C<skipspace>
559 sub inject_if_block {
562 my $linestr = Devel::Declare::get_linestr;
563 if (substr($linestr, $Offset, 1) eq '{') {
564 substr($linestr, $Offset+1, 0) = $inject;
565 Devel::Declare::set_linestr($linestr);
569 =head3 C<scope_injector_call>
571 We want to be able to handle both named and anonymous methods. i.e.
573 method foo () { ... }
574 my $meth = method () { ... };
576 These will then get rewritten as
579 my $meth = method { ... };
581 where 'method' is a subroutine that takes a code block. Spot the problem?
582 The first one doesn't have a semicolon at the end of it! Unlike 'sub' which
583 is a builtin, this is just a normal statement, so we need to terminate it.
584 Luckily, using C<B::Hooks::EndOfScope>, we can do this!
586 use B::Hooks::EndOfScope;
588 We'll add this to what gets 'injected' at the beginning of the method source.
590 sub scope_injector_call {
591 return ' BEGIN { MethodHandlers::inject_scope }; ';
594 So at the beginning of every method, we are passing a callback that will get invoked
595 at the I<end> of the method's compilation... i.e. exactly then the closing C<'}'>
600 my $linestr = Devel::Declare::get_linestr;
601 my $offset = Devel::Declare::get_linestr_offset;
602 substr($linestr, $offset, 0) = ';';
603 Devel::Declare::set_linestr($linestr);
607 =head2 Shadowing each method.
611 We override the current definition of 'method' using C<shadow>.
614 my $pack = Devel::Declare::get_curstash_name;
615 Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]);
618 For a named method we invoked like this:
620 shadow(sub (&) { no strict 'refs'; *{$name} = shift; });
622 So in the case of a C<method foo { ... }>, this call would redefine C<method>
623 to be a subroutine that exports 'sub foo' as the (munged) contents of C<{...}>.
625 The case of an anonymous method is also cute:
627 shadow(sub (&) { shift });
631 my $meth = method () { ... };
633 is rewritten with C<method> taking the codeblock, and returning it as is to become
634 the value of C<$meth>.
636 =head4 C<get_curstash_name>
638 This returns the package name I<currently being compiled>.
642 Handles the details of redefining the subroutine.
646 One of the best ways to learn C<Devel::Declare> is still to look at
649 L<http://cpants.perl.org/dist/used_by/Devel-Declare>.
653 Matt S Trout - E<lt>mst@shadowcat.co.ukE<gt> - original author
655 Company: http://www.shadowcat.co.uk/
656 Blog: http://chainsawblues.vox.com/
658 Florian Ragwitz E<lt>rafl@debian.orgE<gt> - maintainer
660 osfameron E<lt>osfameron@cpan.orgE<gt> - first draft of documentation
662 =head1 COPYRIGHT AND LICENSE
664 This library is free software under the same terms as perl itself
666 Copyright (c) 2007, 2008, 2009 Matt S Trout
668 Copyright (c) 2008, 2009 Florian Ragwitz
670 stolen_chunk_of_toke.c based on toke.c from the perl core, which is
672 Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
673 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others