line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Devel::Declare; |
2
|
|
|
|
|
|
|
|
3
|
31
|
|
|
31
|
|
1339187
|
use strict; |
|
31
|
|
|
|
|
270
|
|
|
31
|
|
|
|
|
930
|
|
4
|
31
|
|
|
31
|
|
159
|
use warnings; |
|
31
|
|
|
|
|
61
|
|
|
31
|
|
|
|
|
789
|
|
5
|
31
|
|
|
31
|
|
718
|
use 5.008001; |
|
31
|
|
|
|
|
124
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.006022'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
bootstrap Devel::Declare; |
10
|
|
|
|
|
|
|
$VERSION =~ tr/_//d; |
11
|
|
|
|
|
|
|
|
12
|
31
|
|
|
31
|
|
233
|
use constant DECLARE_NAME => 1; |
|
31
|
|
|
|
|
83
|
|
|
31
|
|
|
|
|
3681
|
|
13
|
31
|
|
|
31
|
|
217
|
use constant DECLARE_PROTO => 2; |
|
31
|
|
|
|
|
60
|
|
|
31
|
|
|
|
|
1694
|
|
14
|
31
|
|
|
31
|
|
192
|
use constant DECLARE_NONE => 4; |
|
31
|
|
|
|
|
75
|
|
|
31
|
|
|
|
|
1715
|
|
15
|
31
|
|
|
31
|
|
200
|
use constant DECLARE_PACKAGE => 8+1; # name implicit |
|
31
|
|
|
|
|
69
|
|
|
31
|
|
|
|
|
2531
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our (%declarators, %declarator_handlers, @ISA); |
18
|
31
|
|
|
31
|
|
231
|
use base qw(DynaLoader); |
|
31
|
|
|
|
|
57
|
|
|
31
|
|
|
|
|
4871
|
|
19
|
31
|
|
|
31
|
|
225
|
use Scalar::Util 'set_prototype'; |
|
31
|
|
|
|
|
58
|
|
|
31
|
|
|
|
|
2776
|
|
20
|
31
|
|
|
31
|
|
15083
|
use B::Hooks::OP::Check 0.19; |
|
31
|
|
|
|
|
41694
|
|
|
31
|
|
|
|
|
2371
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
@ISA = (); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
initialize(); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub import { |
27
|
18
|
|
|
18
|
|
164
|
my ($class, %args) = @_; |
28
|
18
|
|
|
|
|
44
|
my $target = caller; |
29
|
18
|
100
|
|
|
|
67
|
if (@_ == 1) { # "use Devel::Declare;" |
30
|
31
|
|
|
31
|
|
205
|
no strict 'refs'; |
|
31
|
|
|
|
|
65
|
|
|
31
|
|
|
|
|
17545
|
|
31
|
9
|
|
|
|
|
27
|
foreach my $name (qw(NAME PROTO NONE PACKAGE)) { |
32
|
36
|
|
|
|
|
57
|
*{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"}; |
|
36
|
|
|
|
|
2111
|
|
|
36
|
|
|
|
|
164
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} else { |
35
|
9
|
|
|
|
|
32
|
$class->setup_for($target => \%args); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub unimport { |
40
|
0
|
|
|
0
|
|
0
|
my ($class) = @_; |
41
|
0
|
|
|
|
|
0
|
my $target = caller; |
42
|
0
|
|
|
|
|
0
|
$class->teardown_for($target); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub setup_for { |
46
|
30
|
|
|
30
|
1
|
314606
|
my ($class, $target, $args) = @_; |
47
|
30
|
|
|
|
|
312
|
setup(); |
48
|
30
|
|
|
|
|
134
|
foreach my $key (keys %$args) { |
49
|
30
|
|
|
|
|
76
|
my $info = $args->{$key}; |
50
|
30
|
|
|
|
|
61
|
my ($flags, $sub); |
51
|
30
|
100
|
|
|
|
255
|
if (ref($info) eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
52
|
6
|
|
|
|
|
16
|
($flags, $sub) = @$info; |
53
|
|
|
|
|
|
|
} elsif (ref($info) eq 'CODE') { |
54
|
4
|
|
|
|
|
10
|
$flags = DECLARE_NAME; |
55
|
4
|
|
|
|
|
7
|
$sub = $info; |
56
|
|
|
|
|
|
|
} elsif (ref($info) eq 'HASH') { |
57
|
20
|
|
|
|
|
43
|
$flags = 1; |
58
|
20
|
|
|
|
|
41
|
$sub = $info; |
59
|
|
|
|
|
|
|
} else { |
60
|
0
|
|
|
|
|
0
|
die "Info for sub ${key} must be [ \$flags, \$sub ] or \$sub or handler hashref"; |
61
|
|
|
|
|
|
|
} |
62
|
30
|
|
|
|
|
146
|
$declarators{$target}{$key} = $flags; |
63
|
30
|
|
|
|
|
7247
|
$declarator_handlers{$target}{$key} = $sub; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub teardown_for { |
68
|
0
|
|
|
0
|
0
|
0
|
my ($class, $target) = @_; |
69
|
0
|
|
|
|
|
0
|
delete $declarators{$target}; |
70
|
0
|
|
|
|
|
0
|
delete $declarator_handlers{$target}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $temp_name; |
74
|
|
|
|
|
|
|
my $temp_save; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub init_declare { |
77
|
17
|
|
|
17
|
0
|
48
|
my ($usepack, $use, $inpack, $name, $proto, $traits) = @_; |
78
|
|
|
|
|
|
|
my ($name_h, $XX_h, $extra_code) |
79
|
17
|
|
|
|
|
75
|
= $declarator_handlers{$usepack}{$use}->( |
80
|
|
|
|
|
|
|
$usepack, $use, $inpack, $name, $proto, defined(wantarray), $traits |
81
|
|
|
|
|
|
|
); |
82
|
17
|
|
|
|
|
167
|
($temp_name, $temp_save) = ([], []); |
83
|
17
|
100
|
|
|
|
51
|
if ($name) { |
84
|
11
|
100
|
|
|
|
54
|
$name = "${inpack}::${name}" unless $name =~ /::/; |
85
|
11
|
|
|
|
|
31
|
shadow_sub($name, $name_h); |
86
|
|
|
|
|
|
|
} |
87
|
17
|
100
|
|
|
|
49
|
if ($XX_h) { |
88
|
9
|
|
|
|
|
1959
|
shadow_sub("${inpack}::X", $XX_h); |
89
|
|
|
|
|
|
|
} |
90
|
17
|
50
|
|
|
|
47
|
if (defined wantarray) { |
91
|
17
|
|
100
|
|
|
93
|
return $extra_code || '0;'; |
92
|
|
|
|
|
|
|
} else { |
93
|
0
|
|
|
|
|
0
|
return; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub shadow_sub { |
98
|
79
|
|
|
79
|
1
|
1469
|
my ($name, $cr) = @_; |
99
|
79
|
|
|
|
|
252
|
push(@$temp_name, $name); |
100
|
31
|
|
|
31
|
|
273
|
no strict 'refs'; |
|
31
|
|
|
|
|
73
|
|
|
31
|
|
|
|
|
3159
|
|
101
|
79
|
|
|
|
|
580
|
my ($pack, $pname) = ($name =~ m/(.+)::([^:]+)/); |
102
|
79
|
|
|
|
|
570
|
push(@$temp_save, $pack->can($pname)); |
103
|
31
|
|
|
31
|
|
240
|
no warnings 'redefine'; |
|
31
|
|
|
|
|
76
|
|
|
31
|
|
|
|
|
1581
|
|
104
|
31
|
|
|
31
|
|
194
|
no warnings 'prototype'; |
|
31
|
|
|
|
|
69
|
|
|
31
|
|
|
|
|
3097
|
|
105
|
79
|
|
|
|
|
144
|
*{$name} = $cr; |
|
79
|
|
|
|
|
304
|
|
106
|
79
|
50
|
|
|
|
149
|
set_in_declare(~~@{$temp_name||[]}); |
|
79
|
|
|
|
|
589
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub done_declare { |
110
|
31
|
|
|
31
|
|
231
|
no strict 'refs'; |
|
31
|
|
|
|
|
61
|
|
|
31
|
|
|
|
|
4226
|
|
111
|
79
|
50
|
|
79
|
0
|
150
|
my $name = shift(@{$temp_name||[]}); |
|
79
|
|
|
|
|
223
|
|
112
|
79
|
50
|
|
|
|
203
|
die "done_declare called with no temp_name stack" unless defined($name); |
113
|
79
|
|
|
|
|
124
|
my $saved = shift(@$temp_save); |
114
|
79
|
|
|
|
|
366
|
$name =~ s/(.*):://; |
115
|
79
|
|
|
|
|
231
|
my $temp_pack = $1; |
116
|
79
|
|
|
|
|
130
|
delete ${"${temp_pack}::"}{$name}; |
|
79
|
|
|
|
|
246
|
|
117
|
79
|
100
|
|
|
|
201
|
if ($saved) { |
118
|
31
|
|
|
31
|
|
225
|
no warnings 'prototype'; |
|
31
|
|
|
|
|
62
|
|
|
31
|
|
|
|
|
43605
|
|
119
|
60
|
|
|
|
|
90
|
*{"${temp_pack}::${name}"} = $saved; |
|
60
|
|
|
|
|
305
|
|
120
|
|
|
|
|
|
|
} |
121
|
79
|
50
|
|
|
|
129
|
set_in_declare(~~@{$temp_name||[]}); |
|
79
|
|
|
|
|
12932
|
|
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub build_sub_installer { |
125
|
2
|
|
|
2
|
0
|
93
|
my ($class, $pack, $name, $proto) = @_; |
126
|
2
|
50
|
|
8
|
|
328
|
return eval " |
|
8
|
|
|
|
|
208
|
|
|
0
|
|
|
|
|
0
|
|
|
8
|
|
|
|
|
24
|
|
|
8
|
|
|
|
|
102
|
|
127
|
|
|
|
|
|
|
package ${pack}; |
128
|
|
|
|
|
|
|
my \$body; |
129
|
|
|
|
|
|
|
sub ${name} (${proto}) :lvalue {\n" |
130
|
|
|
|
|
|
|
.' if (wantarray) { |
131
|
|
|
|
|
|
|
goto &$body; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
my $ret = $body->(@_); |
134
|
|
|
|
|
|
|
return $ret; |
135
|
|
|
|
|
|
|
}; |
136
|
|
|
|
|
|
|
sub { ($body) = @_; };'; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub setup_declarators { |
140
|
1
|
|
|
1
|
0
|
20
|
my ($class, $pack, $to_setup) = @_; |
141
|
1
|
50
|
33
|
|
|
18
|
die "${class}->setup_declarators(\$pack, \\\%to_setup)" |
142
|
|
|
|
|
|
|
unless defined($pack) && ref($to_setup) eq 'HASH'; |
143
|
1
|
|
|
|
|
3
|
my %setup_for_args; |
144
|
1
|
|
|
|
|
3
|
foreach my $name (keys %$to_setup) { |
145
|
1
|
|
|
|
|
3
|
my $info = $to_setup->{$name}; |
146
|
1
|
|
50
|
|
|
2
|
my $flags = $info->{flags} || DECLARE_NAME; |
147
|
1
|
|
|
|
|
2
|
my $run = $info->{run}; |
148
|
1
|
|
|
|
|
2
|
my $compile = $info->{compile}; |
149
|
1
|
|
50
|
|
|
5
|
my $proto = $info->{proto} || '&'; |
150
|
1
|
|
|
|
|
2
|
my $sub_proto = $proto; |
151
|
|
|
|
|
|
|
# make all args optional to enable lvalue for DECLARE_NONE |
152
|
1
|
|
|
|
|
3
|
$sub_proto =~ s/;//; $sub_proto = ';'.$sub_proto; |
|
1
|
|
|
|
|
2
|
|
153
|
|
|
|
|
|
|
#my $installer = $class->build_sub_installer($pack, $name, $proto); |
154
|
1
|
|
|
|
|
4
|
my $installer = $class->build_sub_installer($pack, $name, '@'); |
155
|
|
|
|
|
|
|
$installer->(sub :lvalue { |
156
|
|
|
|
|
|
|
#{ no warnings 'uninitialized'; warn 'INST: '.join(', ', @_)."\n"; } |
157
|
7
|
100
|
|
7
|
|
16
|
if (@_) { |
158
|
6
|
100
|
|
|
|
18
|
if (ref $_[0] eq 'HASH') { |
159
|
1
|
|
|
|
|
2
|
shift; |
160
|
1
|
50
|
|
|
|
8
|
if (wantarray) { |
161
|
0
|
|
|
|
|
0
|
my @ret = $run->(undef, undef, @_); |
162
|
0
|
|
|
|
|
0
|
return @ret; |
163
|
|
|
|
|
|
|
} |
164
|
1
|
|
|
|
|
3
|
my $r = $run->(undef, undef, @_); |
165
|
1
|
|
|
|
|
25
|
return $r; |
166
|
|
|
|
|
|
|
} else { |
167
|
5
|
|
|
|
|
101
|
return @_[1..$#_]; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
1
|
|
|
|
|
18
|
return my $sv; |
171
|
1
|
|
|
|
|
23
|
}); |
172
|
|
|
|
|
|
|
$setup_for_args{$name} = [ |
173
|
|
|
|
|
|
|
$flags, |
174
|
|
|
|
|
|
|
sub { |
175
|
7
|
|
|
7
|
|
18
|
my ($usepack, $use, $inpack, $name, $proto, $shift_hashref, $traits) = @_; |
176
|
7
|
|
|
|
|
17
|
my $extra_code = $compile->($name, $proto, $traits); |
177
|
6
|
50
|
|
|
|
164
|
my $main_handler = sub { shift if $shift_hashref; |
178
|
6
|
|
|
|
|
17
|
("DONE", $run->($name, $proto, @_)); |
179
|
7
|
|
|
|
|
82
|
}; |
180
|
7
|
|
|
|
|
12
|
my ($name_h, $XX); |
181
|
7
|
100
|
66
|
|
|
21
|
if (defined $proto) { |
|
|
100
|
|
|
|
|
|
182
|
5
|
|
|
|
|
16
|
$name_h = sub :lvalue { return my $sv; }; |
|
4
|
|
|
|
|
193
|
|
183
|
5
|
|
|
|
|
8
|
$XX = $main_handler; |
184
|
|
|
|
|
|
|
} elsif (defined $name && length $name) { |
185
|
1
|
|
|
|
|
2
|
$name_h = $main_handler; |
186
|
|
|
|
|
|
|
} |
187
|
7
|
|
50
|
|
|
21
|
$extra_code ||= ''; |
188
|
7
|
|
|
|
|
17
|
$extra_code = '}, sub {'.$extra_code; |
189
|
7
|
|
|
|
|
25
|
return ($name_h, $XX, $extra_code); |
190
|
|
|
|
|
|
|
} |
191
|
1
|
|
|
|
|
8
|
]; |
192
|
|
|
|
|
|
|
} |
193
|
1
|
|
|
|
|
5
|
$class->setup_for($pack, \%setup_for_args); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub install_declarator { |
197
|
1
|
|
|
1
|
0
|
87
|
my ($class, $target_pack, $target_name, $flags, $filter, $handler) = @_; |
198
|
1
|
|
|
|
|
13
|
$class->setup_declarators($target_pack, { |
199
|
|
|
|
|
|
|
$target_name => { |
200
|
|
|
|
|
|
|
flags => $flags, |
201
|
|
|
|
|
|
|
compile => $filter, |
202
|
|
|
|
|
|
|
run => $handler, |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
}); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub linestr_callback_rv2cv { |
208
|
17
|
|
|
17
|
0
|
44
|
my ($name, $offset) = @_; |
209
|
17
|
|
|
|
|
44
|
$offset += toke_move_past_token($offset); |
210
|
17
|
|
|
|
|
42
|
my $pack = get_curstash_name(); |
211
|
17
|
|
|
|
|
35
|
my $flags = $declarators{$pack}{$name}; |
212
|
17
|
|
|
|
|
38
|
my ($found_name, $found_proto); |
213
|
17
|
100
|
|
|
|
56
|
if ($flags & DECLARE_NAME) { |
214
|
14
|
|
|
|
|
31
|
$offset += toke_skipspace($offset); |
215
|
14
|
|
|
|
|
34
|
my $linestr = get_linestr(); |
216
|
14
|
100
|
|
|
|
41
|
if (substr($linestr, $offset, 2) eq '::') { |
217
|
11
|
|
|
|
|
23
|
substr($linestr, $offset, 2) = ''; |
218
|
11
|
|
|
|
|
25
|
set_linestr($linestr); |
219
|
|
|
|
|
|
|
} |
220
|
14
|
100
|
|
|
|
55
|
if (my $len = toke_scan_word($offset, $flags & DECLARE_PACKAGE)) { |
221
|
11
|
|
|
|
|
24
|
$found_name = substr($linestr, $offset, $len); |
222
|
11
|
|
|
|
|
23
|
$offset += $len; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
17
|
100
|
|
|
|
62
|
if ($flags & DECLARE_PROTO) { |
226
|
12
|
|
|
|
|
27
|
$offset += toke_skipspace($offset); |
227
|
12
|
|
|
|
|
33
|
my $linestr = get_linestr(); |
228
|
12
|
100
|
|
|
|
36
|
if (substr($linestr, $offset, 1) eq '(') { |
229
|
9
|
|
|
|
|
55
|
my $length = toke_scan_str($offset); |
230
|
9
|
|
|
|
|
32
|
$found_proto = get_lex_stuff(); |
231
|
9
|
|
|
|
|
22
|
clear_lex_stuff(); |
232
|
9
|
100
|
|
|
|
45
|
my $replace = |
233
|
|
|
|
|
|
|
($found_name ? ' ' : '=') |
234
|
|
|
|
|
|
|
.'X'.(' ' x length($found_proto)); |
235
|
9
|
|
|
|
|
34
|
$linestr = get_linestr(); |
236
|
9
|
|
|
|
|
18
|
substr($linestr, $offset, $length) = $replace; |
237
|
9
|
|
|
|
|
22
|
set_linestr($linestr); |
238
|
9
|
|
|
|
|
15
|
$offset += $length; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
} |
241
|
17
|
|
|
|
|
68
|
my @args = ($pack, $name, $pack, $found_name, $found_proto); |
242
|
17
|
|
|
|
|
47
|
$offset += toke_skipspace($offset); |
243
|
17
|
|
|
|
|
43
|
my $linestr = get_linestr(); |
244
|
17
|
50
|
|
|
|
47
|
if (substr($linestr, $offset, 1) eq '{') { |
245
|
17
|
|
|
|
|
42
|
my $ret = init_declare(@args); |
246
|
17
|
|
|
|
|
35
|
$offset++; |
247
|
17
|
50
|
33
|
|
|
109
|
if (defined $ret && length $ret) { |
248
|
17
|
|
|
|
|
51
|
substr($linestr, $offset, 0) = $ret; |
249
|
17
|
|
|
|
|
1776
|
set_linestr($linestr); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
} else { |
252
|
0
|
|
|
|
|
0
|
init_declare(@args); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
#warn "linestr now ${linestr}"; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub linestr_callback_const { |
258
|
17
|
|
|
17
|
0
|
70
|
my ($name, $offset) = @_; |
259
|
17
|
|
|
|
|
53
|
my $pack = get_curstash_name(); |
260
|
17
|
|
|
|
|
36
|
my $flags = $declarators{$pack}{$name}; |
261
|
17
|
100
|
|
|
|
83
|
if ($flags & DECLARE_NAME) { |
262
|
14
|
|
|
|
|
45
|
$offset += toke_move_past_token($offset); |
263
|
14
|
|
|
|
|
46
|
$offset += toke_skipspace($offset); |
264
|
14
|
100
|
|
|
|
72
|
if (toke_scan_word($offset, $flags & DECLARE_PACKAGE)) { |
265
|
11
|
|
|
|
|
39
|
my $linestr = get_linestr(); |
266
|
11
|
|
|
|
|
43
|
substr($linestr, $offset, 0) = '::'; |
267
|
11
|
|
|
|
|
70
|
set_linestr($linestr); |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub linestr_callback { |
273
|
126
|
|
|
126
|
0
|
8926
|
my $type = shift; |
274
|
126
|
|
|
|
|
241
|
my $name = $_[0]; |
275
|
126
|
|
|
|
|
355
|
my $pack = get_curstash_name(); |
276
|
126
|
|
|
|
|
257
|
my $handlers = $declarator_handlers{$pack}{$name}; |
277
|
126
|
100
|
|
|
|
428
|
if (ref $handlers eq 'CODE') { |
|
|
50
|
|
|
|
|
|
278
|
34
|
|
|
|
|
73
|
my $meth = "linestr_callback_${type}"; |
279
|
34
|
|
|
|
|
258
|
__PACKAGE__->can($meth)->(@_); |
280
|
|
|
|
|
|
|
} elsif (ref $handlers eq 'HASH') { |
281
|
92
|
100
|
|
|
|
6784
|
if ($handlers->{$type}) { |
282
|
76
|
|
|
|
|
275
|
$handlers->{$type}->(@_); |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
} else { |
285
|
0
|
|
|
|
|
0
|
die "PANIC: unknown thing in handlers for $pack $name: $handlers"; |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head1 NAME |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Devel::Declare - (DEPRECATED) Adding keywords to perl, in perl |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 SYNOPSIS |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
use Method::Signatures; |
296
|
|
|
|
|
|
|
# or ... |
297
|
|
|
|
|
|
|
use MooseX::Declare; |
298
|
|
|
|
|
|
|
# etc. |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
# Use some new and exciting syntax like: |
301
|
|
|
|
|
|
|
method hello (Str :$who, Int :$age where { $_ > 0 }) { |
302
|
|
|
|
|
|
|
$self->say("Hello ${who}, I am ${age} years old!"); |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 DESCRIPTION |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
L can install subroutines called declarators which locally take |
308
|
|
|
|
|
|
|
over Perl's parser, allowing the creation of new syntax. |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
This document describes how to create a simple declarator. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=head1 WARNING |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=for comment mst wrote this warning for MooseX::Declare, and ether adapted it for here: |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
B Devel::Declare is a giant bag of crack |
317
|
|
|
|
|
|
|
originally implemented by mst with the goal of upsetting the perl core |
318
|
|
|
|
|
|
|
developers so much by its very existence that they implemented proper |
319
|
|
|
|
|
|
|
keyword handling in the core. |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
As of perl5 version 14, this goal has been achieved, and modules such |
322
|
|
|
|
|
|
|
as L, L, and L provide |
323
|
|
|
|
|
|
|
mechanisms to mangle perl syntax that don't require hallucinogenic |
324
|
|
|
|
|
|
|
drugs to interpret the error messages they produce. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
If you are using something that uses Devel::Declare, please for the love |
327
|
|
|
|
|
|
|
of kittens use something else: |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=over 4 |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=item * |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
Instead of L, use L |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=item * |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
Instead of L, use |
338
|
|
|
|
|
|
|
L (requires perl 5.22) or L |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=back |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=head1 USAGE |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
We'll demonstrate the usage of C with a motivating example: a new |
345
|
|
|
|
|
|
|
C keyword, which acts like the builtin C, but automatically unpacks |
346
|
|
|
|
|
|
|
C<$self> and the other arguments. |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
package My::Methods; |
349
|
|
|
|
|
|
|
use Devel::Declare; |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=head2 Creating a declarator with C |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
You will typically create |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub import { |
356
|
|
|
|
|
|
|
my $class = shift; |
357
|
|
|
|
|
|
|
my $caller = caller; |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
Devel::Declare->setup_for( |
360
|
|
|
|
|
|
|
$caller, |
361
|
|
|
|
|
|
|
{ method => { const => \&parser } } |
362
|
|
|
|
|
|
|
); |
363
|
|
|
|
|
|
|
no strict 'refs'; |
364
|
|
|
|
|
|
|
*{$caller.'::method'} = sub (&) {}; |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
Starting from the end of this import routine, you'll see that we're creating a |
368
|
|
|
|
|
|
|
subroutine called C in the caller's namespace. Yes, that's just a normal |
369
|
|
|
|
|
|
|
subroutine, and it does nothing at all (yet!) Note the prototype C<(&)> which means |
370
|
|
|
|
|
|
|
that the caller would call it like so: |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
method { |
373
|
|
|
|
|
|
|
my ($self, $arg1, $arg2) = @_; |
374
|
|
|
|
|
|
|
... |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
However we want to be able to call it like this |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
method foo ($arg1, $arg2) { |
380
|
|
|
|
|
|
|
... |
381
|
|
|
|
|
|
|
} |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
That's why we call C above, to register the declarator 'method' with a custom |
384
|
|
|
|
|
|
|
parser, as per the next section. It acts on an optype, usually C<'const'> as above. |
385
|
|
|
|
|
|
|
(Other valid values are C<'check'> and C<'rv2cv'>). |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
For a simpler way to install new methods, see also L |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=head2 Writing a parser subroutine |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
This subroutine is called at I time, and allows you to read the custom |
392
|
|
|
|
|
|
|
syntaxes that we want (in a syntax that may or may not be valid core Perl 5) and |
393
|
|
|
|
|
|
|
munge it so that the result will be parsed by the C compiler. |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
For this example, we're defining some globals for convenience: |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
our ($Declarator, $Offset); |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
Then we define a parser subroutine to handle our declarator. We'll look at this in |
400
|
|
|
|
|
|
|
a few chunks. |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
sub parser { |
403
|
|
|
|
|
|
|
local ($Declarator, $Offset) = @_; |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
C provides some very low level utility methods to parse character |
406
|
|
|
|
|
|
|
strings. We'll define some useful higher level routines below for convenience, |
407
|
|
|
|
|
|
|
and we can use these to parse the various elements in our new syntax. |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
Notice how our parser subroutine is invoked at compile time, |
410
|
|
|
|
|
|
|
when the C parser is pointed just I the declarator name. |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
skip_declarator; # step past 'method' |
413
|
|
|
|
|
|
|
my $name = strip_name; # strip out the name 'foo', if present |
414
|
|
|
|
|
|
|
my $proto = strip_proto; # strip out the prototype '($arg1, $arg2)', if present |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
Now we can prepare some code to 'inject' into the new subroutine. For example we |
417
|
|
|
|
|
|
|
might want the method as above to have C injected at |
418
|
|
|
|
|
|
|
the beginning of it. We also do some clever stuff with scopes that we'll look |
419
|
|
|
|
|
|
|
at shortly. |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
my $inject = make_proto_unwrap($proto); |
422
|
|
|
|
|
|
|
if (defined $name) { |
423
|
|
|
|
|
|
|
$inject = scope_injector_call().$inject; |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
inject_if_block($inject); |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
We've now managed to change C into C
|
428
|
|
|
|
|
|
|
injected_code; ... }>. This will compile... but we've lost the name of the |
429
|
|
|
|
|
|
|
method! |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
In a cute (or horrifying, depending on your perspective) trick, we temporarily |
432
|
|
|
|
|
|
|
change the definition of the subroutine C itself, to specialise it with |
433
|
|
|
|
|
|
|
the C<$name> we stripped, so that it assigns the code block to that name. |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
Even though the I time C is compiled, it will be |
436
|
|
|
|
|
|
|
redefined again, C caches these definitions in its parse |
437
|
|
|
|
|
|
|
tree, so we'll always get the right one! |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
Note that we also handle the case where there was no name, allowing |
440
|
|
|
|
|
|
|
an anonymous method analogous to an anonymous subroutine. |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
if (defined $name) { |
443
|
|
|
|
|
|
|
$name = join('::', Devel::Declare::get_curstash_name(), $name) |
444
|
|
|
|
|
|
|
unless ($name =~ /::/); |
445
|
|
|
|
|
|
|
shadow(sub (&) { no strict 'refs'; *{$name} = shift; }); |
446
|
|
|
|
|
|
|
} else { |
447
|
|
|
|
|
|
|
shadow(sub (&) { shift }); |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
} |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
=head2 Parser utilities in detail |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
For simplicity, we're using global variables like C<$Offset> in these examples. |
455
|
|
|
|
|
|
|
You may prefer to look at L, which |
456
|
|
|
|
|
|
|
encapsulates the context much more cleanly. |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
=head3 C |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
This simple parser just moves across a 'token'. The common case is |
461
|
|
|
|
|
|
|
to skip the declarator, i.e. to move to the end of the string |
462
|
|
|
|
|
|
|
'method' and before the prototype and code block. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
sub skip_declarator { |
465
|
|
|
|
|
|
|
$Offset += Devel::Declare::toke_move_past_token($Offset); |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=head4 C |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
This builtin parser simply moves past a 'token' (matching C[a-zA-Z_]\w*/>) |
471
|
|
|
|
|
|
|
It takes an offset into the source document, and skips past the token. |
472
|
|
|
|
|
|
|
It returns the number of characters skipped. |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
=head3 C |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
This parser skips any whitespace, then scans the next word (again matching a |
477
|
|
|
|
|
|
|
'token'). We can then analyse the current line, and manipulate it (using pure |
478
|
|
|
|
|
|
|
Perl). In this case we take the name of the method out, and return it. |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
sub strip_name { |
481
|
|
|
|
|
|
|
skipspace; |
482
|
|
|
|
|
|
|
if (my $len = Devel::Declare::toke_scan_word($Offset, 1)) { |
483
|
|
|
|
|
|
|
my $linestr = Devel::Declare::get_linestr(); |
484
|
|
|
|
|
|
|
my $name = substr($linestr, $Offset, $len); |
485
|
|
|
|
|
|
|
substr($linestr, $Offset, $len) = ''; |
486
|
|
|
|
|
|
|
Devel::Declare::set_linestr($linestr); |
487
|
|
|
|
|
|
|
return $name; |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
return; |
490
|
|
|
|
|
|
|
} |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
=head4 C |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
This builtin parser, given an offset into the source document, |
495
|
|
|
|
|
|
|
matches a 'token' as above but does not skip. It returns the |
496
|
|
|
|
|
|
|
length of the token matched, if any. |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
=head4 C |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
This builtin returns the full text of the current line of the source document. |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=head4 C |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
This builtin sets the full text of the current line of the source document. |
505
|
|
|
|
|
|
|
Beware that injecting a newline into the middle of the line is likely |
506
|
|
|
|
|
|
|
to fail in surprising ways. Generally, Perl's parser can rely on the |
507
|
|
|
|
|
|
|
`current line' actually being only a single line. Use other kinds of |
508
|
|
|
|
|
|
|
whitespace instead, in the code that you inject. |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=head3 C |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
This parser skips whitsepace. |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
sub skipspace { |
515
|
|
|
|
|
|
|
$Offset += Devel::Declare::toke_skipspace($Offset); |
516
|
|
|
|
|
|
|
} |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=head4 C |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
This builtin parser, given an offset into the source document, |
521
|
|
|
|
|
|
|
skips over any whitespace, and returns the number of characters |
522
|
|
|
|
|
|
|
skipped. |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=head3 C |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
This is a more complex parser that checks if it's found something that |
527
|
|
|
|
|
|
|
starts with C<'('> and returns everything till the matching C<')'>. |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
sub strip_proto { |
530
|
|
|
|
|
|
|
skipspace; |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
my $linestr = Devel::Declare::get_linestr(); |
533
|
|
|
|
|
|
|
if (substr($linestr, $Offset, 1) eq '(') { |
534
|
|
|
|
|
|
|
my $length = Devel::Declare::toke_scan_str($Offset); |
535
|
|
|
|
|
|
|
my $proto = Devel::Declare::get_lex_stuff(); |
536
|
|
|
|
|
|
|
Devel::Declare::clear_lex_stuff(); |
537
|
|
|
|
|
|
|
$linestr = Devel::Declare::get_linestr(); |
538
|
|
|
|
|
|
|
substr($linestr, $Offset, $length) = ''; |
539
|
|
|
|
|
|
|
Devel::Declare::set_linestr($linestr); |
540
|
|
|
|
|
|
|
return $proto; |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
return; |
543
|
|
|
|
|
|
|
} |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
=head4 C |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
This builtin parser uses Perl's own parsing routines to match a "stringlike" |
548
|
|
|
|
|
|
|
expression. Handily, this includes bracketed expressions (just think about |
549
|
|
|
|
|
|
|
things like C). |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
Also it Does The Right Thing with nested delimiters (like C). |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
It returns the effective length of the expression matched. Really, what |
554
|
|
|
|
|
|
|
it returns is the difference in position between where the string started, |
555
|
|
|
|
|
|
|
within the buffer, and where it finished. If the string extended across |
556
|
|
|
|
|
|
|
multiple lines then the contents of the buffer may have been completely |
557
|
|
|
|
|
|
|
replaced by the new lines, so this position difference is not the same |
558
|
|
|
|
|
|
|
thing as the actual length of the expression matched. However, because |
559
|
|
|
|
|
|
|
moving backward in the buffer causes problems, the function arranges |
560
|
|
|
|
|
|
|
for the effective length to always be positive, padding the start of |
561
|
|
|
|
|
|
|
the buffer if necessary. |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
Use C to get the actual matched text, the content of |
564
|
|
|
|
|
|
|
the string. Because of the behaviour around multiline strings, you |
565
|
|
|
|
|
|
|
can't reliably get this from the buffer. In fact, after the function |
566
|
|
|
|
|
|
|
returns, you can't rely on any content of the buffer preceding the end |
567
|
|
|
|
|
|
|
of the string. |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
If the string being scanned is not well formed (has no closing delimiter), |
570
|
|
|
|
|
|
|
C returns C. In this case you cannot rely on the |
571
|
|
|
|
|
|
|
contents of the buffer. |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
=head4 C |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
This builtin returns what was matched by C. To avoid segfaults, |
576
|
|
|
|
|
|
|
you should call C immediately afterwards. |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
=head2 Munging the subroutine |
579
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
Let's look at what we need to do in detail. |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=head3 C |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
We may have defined our method in different ways, which will result |
585
|
|
|
|
|
|
|
in a different value for our prototype, as parsed above. For example: |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
method foo { # undefined |
588
|
|
|
|
|
|
|
method foo () { # '' |
589
|
|
|
|
|
|
|
method foo ($arg1) { # '$arg1' |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
We deal with them as follows, and return the appropriate C |
592
|
|
|
|
|
|
|
string. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
sub make_proto_unwrap { |
595
|
|
|
|
|
|
|
my ($proto) = @_; |
596
|
|
|
|
|
|
|
my $inject = 'my ($self'; |
597
|
|
|
|
|
|
|
if (defined $proto) { |
598
|
|
|
|
|
|
|
$inject .= ", $proto" if length($proto); |
599
|
|
|
|
|
|
|
$inject .= ') = @_; '; |
600
|
|
|
|
|
|
|
} else { |
601
|
|
|
|
|
|
|
$inject .= ') = shift;'; |
602
|
|
|
|
|
|
|
} |
603
|
|
|
|
|
|
|
return $inject; |
604
|
|
|
|
|
|
|
} |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
=head3 C |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
Now we need to inject it after the opening C<'{'> of the method body. |
609
|
|
|
|
|
|
|
We can do this with the building blocks we defined above like C |
610
|
|
|
|
|
|
|
and C. |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
sub inject_if_block { |
613
|
|
|
|
|
|
|
my $inject = shift; |
614
|
|
|
|
|
|
|
skipspace; |
615
|
|
|
|
|
|
|
my $linestr = Devel::Declare::get_linestr; |
616
|
|
|
|
|
|
|
if (substr($linestr, $Offset, 1) eq '{') { |
617
|
|
|
|
|
|
|
substr($linestr, $Offset+1, 0) = $inject; |
618
|
|
|
|
|
|
|
Devel::Declare::set_linestr($linestr); |
619
|
|
|
|
|
|
|
} |
620
|
|
|
|
|
|
|
} |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
=head3 C |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
We want to be able to handle both named and anonymous methods. i.e. |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
method foo () { ... } |
627
|
|
|
|
|
|
|
my $meth = method () { ... }; |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
These will then get rewritten as |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
method { ... } |
632
|
|
|
|
|
|
|
my $meth = method { ... }; |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
where 'method' is a subroutine that takes a code block. Spot the problem? |
635
|
|
|
|
|
|
|
The first one doesn't have a semicolon at the end of it! Unlike 'sub' which |
636
|
|
|
|
|
|
|
is a builtin, this is just a normal statement, so we need to terminate it. |
637
|
|
|
|
|
|
|
Luckily, using C, we can do this! |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
use B::Hooks::EndOfScope; |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
We'll add this to what gets 'injected' at the beginning of the method source. |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
sub scope_injector_call { |
644
|
|
|
|
|
|
|
return ' BEGIN { MethodHandlers::inject_scope }; '; |
645
|
|
|
|
|
|
|
} |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
So at the beginning of every method, we are passing a callback that will get invoked |
648
|
|
|
|
|
|
|
at the I of the method's compilation... i.e. exactly then the closing C<'}'> |
649
|
|
|
|
|
|
|
is compiled. |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
sub inject_scope { |
652
|
|
|
|
|
|
|
on_scope_end { |
653
|
|
|
|
|
|
|
my $linestr = Devel::Declare::get_linestr; |
654
|
|
|
|
|
|
|
my $offset = Devel::Declare::get_linestr_offset; |
655
|
|
|
|
|
|
|
substr($linestr, $offset, 0) = ';'; |
656
|
|
|
|
|
|
|
Devel::Declare::set_linestr($linestr); |
657
|
|
|
|
|
|
|
}; |
658
|
|
|
|
|
|
|
} |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
=head2 Shadowing each method. |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
=head3 C |
663
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
We override the current definition of 'method' using C. |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
sub shadow { |
667
|
|
|
|
|
|
|
my $pack = Devel::Declare::get_curstash_name; |
668
|
|
|
|
|
|
|
Devel::Declare::shadow_sub("${pack}::${Declarator}", $_[0]); |
669
|
|
|
|
|
|
|
} |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
For a named method we invoked like this: |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
shadow(sub (&) { no strict 'refs'; *{$name} = shift; }); |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
So in the case of a C, this call would redefine C |
676
|
|
|
|
|
|
|
to be a subroutine that exports 'sub foo' as the (munged) contents of C<{...}>. |
677
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
The case of an anonymous method is also cute: |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
shadow(sub (&) { shift }); |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
This means that |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
my $meth = method () { ... }; |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
is rewritten with C taking the codeblock, and returning it as is to become |
687
|
|
|
|
|
|
|
the value of C<$meth>. |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=head4 C |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
This returns the package name I. |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
=head4 C |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
Handles the details of redefining the subroutine. |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=head1 SEE ALSO |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
One of the best ways to learn C is still to look at |
700
|
|
|
|
|
|
|
modules that use it: |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
L. |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
=head1 AUTHORS |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
Matt S Trout - Emst@shadowcat.co.ukE - original author |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
Company: http://www.shadowcat.co.uk/ |
709
|
|
|
|
|
|
|
Blog: http://chainsawblues.vox.com/ |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
Florian Ragwitz Erafl@debian.orgE - maintainer |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
osfameron Eosfameron@cpan.orgE - first draft of documentation |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
This library is free software under the same terms as perl itself |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
Copyright (c) 2007, 2008, 2009 Matt S Trout |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
Copyright (c) 2008, 2009 Florian Ragwitz |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
stolen_chunk_of_toke.c based on toke.c from the perl core, which is |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
726
|
|
|
|
|
|
|
2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
=cut |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
1; |